Index: mod/feedback/item/label/lib.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/feedback/item/label/lib.php,v retrieving revision 1.17 diff -u -r1.17 lib.php --- mod/feedback/item/label/lib.php 4 Jun 2010 21:12:28 -0000 1.17 +++ mod/feedback/item/label/lib.php 28 Jun 2010 22:32:41 -0000 @@ -9,7 +9,7 @@ var $item_form; var $context; var $item; - + function init() { global $CFG; $this->presentationoptions = array('maxfiles' => EDITOR_UNLIMITED_FILES, 'trusttext'=>true); @@ -43,8 +43,8 @@ 'feedback'=>$feedback->id); $this->context = get_context_instance(CONTEXT_MODULE, $cm->id); - - + + //preparing the editor for new file-api $item->presentationformat = FORMAT_HTML; $item->presentationtrust = 1; @@ -52,18 +52,19 @@ 'presentation', //name of the form element $this->presentationoptions, $this->context, - 'feedback_item', //the filearea + 'mod_feedback', + 'item', //the filearea $item->id); - + //build the form $this->item_form = new feedback_label_form('edit_item.php', array('item'=>$item, 'common'=>$commonparams, 'positionlist'=>$positionlist, 'position'=>$position, 'presentationoptions'=>$this->presentationoptions)); } - + //this function only can used after the call of build_editform() function show_editform() { $this->item_form->display(); } - + function is_cancelled() { return $this->item_form->is_cancelled(); } @@ -77,37 +78,38 @@ function save_item() { global $DB; - + if(!$item = $this->item_form->get_data()) { return false; } - + if($item->clone_item) { $item->id = ''; //to clone this item $item->position++; } $item->presentation = ''; - + $item->hasvalue = $this->get_hasvalue(); if(!$item->id) { $item->id = $DB->insert_record('feedback_item', $item); }else { $DB->update_record('feedback_item', $item); } - + $item = file_postupdate_standard_editor($item, 'presentation', $this->presentationoptions, $this->context, - 'feedback_item', + 'mod_feedback', + 'item', $item->id); - + $DB->update_record('feedback_item', $item); - + return $DB->get_record('feedback_item', array('id'=>$item->id)); } - + function print_item($item){ global $DB; @@ -115,21 +117,21 @@ if(!$item->feedback AND $item->template) { $template = $DB->get_record('feedback_template', array('id'=>$item->template)); $context = get_context_instance(CONTEXT_COURSE, $template->course); - $filearea = 'course_summary'; + $filearea = 'template'; }else { $cm = get_coursemodule_from_instance('feedback', $item->feedback); $context = get_context_instance(CONTEXT_MODULE, $cm->id); - $filearea = 'feedback_item'; + $filearea = 'item'; } - + $item->presentationformat = FORMAT_HTML; $item->presentationtrust = 1; - - $output = file_rewrite_pluginfile_urls($item->presentation, 'pluginfile.php', $context->id, $filearea, $item->id); + + $output = file_rewrite_pluginfile_urls($item->presentation, 'pluginfile.php', $context->id, 'mod_feedback', $filearea, $item->id); echo format_text($output, FORMAT_HTML); } - /** + /** * print the item at the edit-page of feedback * * @global object @@ -138,7 +140,7 @@ */ function print_item_preview($item) { global $OUTPUT, $DB; - + if($item->dependitem) { if($dependitem = $DB->get_record('feedback_item', array('id'=>$item->dependitem))) { echo ' ('.$dependitem->label.'->'.$item->dependvalue.')'; @@ -146,8 +148,8 @@ } $this->print_item($item); } - - /** + + /** * print the item at the complete-page of feedback * * @global object @@ -160,7 +162,7 @@ $this->print_item($item); } - /** + /** * print the item at the complete-page of feedback * * @global object @@ -179,19 +181,19 @@ function compare_value($item, $dbvalue, $dependvalue) { return false; } - + //used by create_item and update_item functions, //when provided $data submitted from feedback_show_edit function get_presentation($data) { // $context = get_context_instance(CONTEXT_MODULE, $data->cmid); - + // $presentation = new object(); // $presentation->id = null; // $presentation->definition = ''; // $presentation->format = FORMAT_HTML; - + // $draftid_editor = file_get_submitted_draft_itemid('presentation'); - // $currenttext = file_prepare_draft_area($draftid_editor, $context->id, 'feedback_item_label', $presentation->id, array('subdirs'=>true), $presentation->definition); + // $currenttext = file_prepare_draft_area($draftid_editor, $context->id, 'mod_feedback', 'item_label', $presentation->id, array('subdirs'=>true), $presentation->definition); // $presentation->entry = array('text'=>$currenttext, 'format'=>$presentation->format, 'itemid'=>$draftid_editor); // return $data->presentation; @@ -199,9 +201,9 @@ function postupdate($item) { global $DB; - + $context = get_context_instance(CONTEXT_MODULE, $item->cmid); - $item = file_postupdate_standard_editor($item, 'presentation', $this->presentationoptions, $context, 'feedback_item', $item->id); + $item = file_postupdate_standard_editor($item, 'presentation', $this->presentationoptions, $context, 'mod_feedback', 'item', $item->id); // $item = new object(); // $item->id = $data->id @@ -210,11 +212,11 @@ } return false; } - + function get_hasvalue() { return 0; } - + function can_switch_require() { return false; } Index: course/editcategory.php =================================================================== RCS file: /cvsroot/moodle/moodle/course/editcategory.php,v retrieving revision 1.31 diff -u -r1.31 editcategory.php --- course/editcategory.php 16 Jan 2010 15:39:59 -0000 1.31 +++ course/editcategory.php 28 Jun 2010 22:31:53 -0000 @@ -42,7 +42,7 @@ } $editoroptions = array('maxfiles' => EDITOR_UNLIMITED_FILES, 'maxbytes'=>$CFG->maxbytes, 'trusttext'=>true); -$category = file_prepare_standard_editor($category, 'description', $editoroptions, $editorcontext, 'category_description', $category->id); +$category = file_prepare_standard_editor($category, 'description', $editoroptions, $editorcontext, 'coursecat', 'description', 0); $mform = new editcategory_form('editcategory.php', compact('category', 'editoroptions')); $mform->set_data($category); @@ -82,7 +82,7 @@ mark_context_dirty($newcategory->context->path); } - $newcategory = file_postupdate_standard_editor($newcategory, 'description', $editoroptions, $categorycontext, 'category_description', $newcategory->id); + $newcategory = file_postupdate_standard_editor($newcategory, 'description', $editoroptions, $categorycontext, 'coursecat', 'description', 0); $DB->update_record('course_categories', $newcategory); fix_course_sortorder(); Index: course/edit.php =================================================================== RCS file: /cvsroot/moodle/moodle/course/edit.php,v retrieving revision 1.130 diff -u -r1.130 edit.php --- course/edit.php 21 Jun 2010 15:30:51 -0000 1.130 +++ course/edit.php 28 Jun 2010 22:31:53 -0000 @@ -74,10 +74,10 @@ } } $course->allowedmods = $allowedmods; - $course = file_prepare_standard_editor($course, 'summary', $editoroptions, $coursecontext, 'course_summary', 0); + $course = file_prepare_standard_editor($course, 'summary', $editoroptions, $coursecontext, 'course', 'summary', 0); } else { - $course = file_prepare_standard_editor($course, 'summary', $editoroptions, null, 'course_summary', null); + $course = file_prepare_standard_editor($course, 'summary', $editoroptions, null, 'course', 'summary', null); } // first create the form Index: course/modedit.php =================================================================== RCS file: /cvsroot/moodle/moodle/course/modedit.php,v retrieving revision 1.100 diff -u -r1.100 modedit.php --- course/modedit.php 22 Jun 2010 07:05:11 -0000 1.100 +++ course/modedit.php 28 Jun 2010 22:31:56 -0000 @@ -79,7 +79,7 @@ if (plugin_supports('mod', $data->modulename, FEATURE_MOD_INTRO, true)) { $draftid_editor = file_get_submitted_draft_itemid('introeditor'); - file_prepare_draft_area($draftid_editor, null, null, null); + file_prepare_draft_area($draftid_editor, null, null, null, null); $data->introeditor = array('text'=>'', 'format'=>FORMAT_HTML, 'itemid'=>$draftid_editor); // TODO: add better default } @@ -140,7 +140,7 @@ if (plugin_supports('mod', $data->modulename, FEATURE_MOD_INTRO, true)) { $draftid_editor = file_get_submitted_draft_itemid('introeditor'); - $currentintro = file_prepare_draft_area($draftid_editor, $context->id, $data->modulename.'_intro', 0, array('subdirs'=>true), $data->intro); + $currentintro = file_prepare_draft_area($draftid_editor, $context->id, 'mod_'.$data->modulename, 'intro', 0, array('subdirs'=>true), $data->intro); $data->introeditor = array('text'=>$currentintro, 'format'=>$data->introformat, 'itemid'=>$draftid_editor); } @@ -321,7 +321,7 @@ // update embedded links and save files if (plugin_supports('mod', $fromform->modulename, FEATURE_MOD_INTRO, true)) { $fromform->intro = file_save_draft_area_files($fromform->introeditor['itemid'], $modcontext->id, - $fromform->modulename.'_intro', 0, + 'mod_'.$fromform->modulename, 'intro', 0, array('subdirs'=>true), $fromform->introeditor['text']); $fromform->introformat = $fromform->introeditor['format']; unset($fromform->introeditor); @@ -425,7 +425,7 @@ $modcontext = get_context_instance(CONTEXT_MODULE, $fromform->coursemodule); if (plugin_supports('mod', $fromform->modulename, FEATURE_MOD_INTRO, true)) { $fromform->intro = file_save_draft_area_files($introeditor['itemid'], $modcontext->id, - $fromform->modulename.'_intro', 0, + 'mod_'.$fromform->modulename, 'intro', 0, array('subdirs'=>true), $introeditor['text']); $DB->set_field($fromform->modulename, 'intro', $fromform->intro, array('id'=>$fromform->instance)); } Index: course/scales.php =================================================================== RCS file: /cvsroot/moodle/moodle/course/scales.php,v retrieving revision 1.52 diff -u -r1.52 scales.php --- course/scales.php 27 Jun 2010 21:34:02 -0000 1.52 +++ course/scales.php 28 Jun 2010 22:31:56 -0000 @@ -84,7 +84,7 @@ foreach ($scales as $scale) { - $scale->description = file_rewrite_pluginfile_urls($scale->description, 'pluginfile.php', $systemcontext->id, 'grade_scale', $scale->id); + $scale->description = file_rewrite_pluginfile_urls($scale->description, 'pluginfile.php', $systemcontext->id, 'grade', 'scale', $scale->id); $scalemenu = make_menu_from_list($scale->scale); @@ -110,7 +110,7 @@ echo $OUTPUT->heading($strstandardscales); foreach ($scales as $scale) { - $scale->description = file_rewrite_pluginfile_urls($scale->description, 'pluginfile.php', $systemcontext->id, 'grade_scale', $scale->id); + $scale->description = file_rewrite_pluginfile_urls($scale->description, 'pluginfile.php', $systemcontext->id, 'grade', 'scale', $scale->id); $scalemenu = make_menu_from_list($scale->scale); Index: course/lib.php =================================================================== RCS file: /cvsroot/moodle/moodle/course/lib.php,v retrieving revision 1.745 diff -u -r1.745 lib.php --- course/lib.php 24 Jun 2010 05:58:42 -0000 1.745 +++ course/lib.php 28 Jun 2010 22:31:56 -0000 @@ -2226,7 +2226,7 @@ $context = get_context_instance(CONTEXT_COURSE, $course->id); // Rewrite file URLs so that they are correct - $course->summary = file_rewrite_pluginfile_urls($course->summary, 'pluginfile.php', $context->id, 'course_summary', NULL); + $course->summary = file_rewrite_pluginfile_urls($course->summary, 'pluginfile.php', $context->id, 'course', 'summary', NULL); $linkcss = $course->visible ? '' : ' class="dimmed" '; @@ -3483,7 +3483,7 @@ if ($editoroptions) { // Save the files used in the summary editor and store - $data = file_postupdate_standard_editor($data, 'summary', $editoroptions, $context, 'course_summary', 0); + $data = file_postupdate_standard_editor($data, 'summary', $editoroptions, $context, 'course', 'summary', 0); $DB->set_field('course', 'summary', $data->summary, array('id'=>$newcourseid)); $DB->set_field('course', 'summaryformat', $data->summary_format, array('id'=>$newcourseid)); } @@ -3548,7 +3548,7 @@ $context = get_context_instance(CONTEXT_COURSE, $oldcourse->id); if ($editoroptions) { - $data = file_postupdate_standard_editor($data, 'summary', $editoroptions, $context, 'course_summary', 0); + $data = file_postupdate_standard_editor($data, 'summary', $editoroptions, $context, 'course', 'summary', 0); } if (!isset($data->category) or empty($data->category)) { @@ -3631,6 +3631,9 @@ * This class pertains to course requests and contains methods associated with * create, approving, and removing course requests. * + * Please note we do not allow embedded images here because there is no context + * to store them with proper access control. + * * @copyright 2009 Sam Hemelryk * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @since Moodle 2.0 @@ -3662,22 +3665,6 @@ protected static $summaryeditoroptions; /** - * The context used when working with files for the summary editor - * This is initially set by {@link summary_editor_context()} - * @var stdClass - * @static - */ - protected static $summaryeditorcontext; - - /** - * The string used to identify the file area for course_requests - * This is initially set by {@link summary_editor_context()} - * @var string - * @static - */ - protected static $summaryeditorfilearea = 'course_request_summary'; - - /** * Static function to prepare the summary editor for working with a course * request. * @@ -3692,7 +3679,7 @@ if ($data === null) { $data = new stdClass; } - $data = file_prepare_standard_editor($data, 'summary', self::summary_editor_options(), self::summary_editor_context(), self::summary_editor_filearea(), null); + $data = file_prepare_standard_editor($data, 'summary', self::summary_editor_options()); return $data; } @@ -3709,19 +3696,13 @@ public static function create($data) { global $USER, $DB, $CFG; $data->requester = $USER->id; - $editorused = (!empty($data->summary_editor)); - // Has summary_editor been set. If so we have come through with a editor and - // may need to save files - if ($editorused && empty($data->summary)) { - // Summary is a required field so copy the text over - $data->summary = $data->summary_editor['text']; - } + + // Summary is a required field so copy the text over + $data->summary = $data->summary_editor['text']; + $data->summaryformat = $data->summary_editor['format']; + $data->id = $DB->insert_record('course_request', $data); - if ($editorused) { - // Save any files and then update the course with the fixed data - $data = file_postupdate_standard_editor($data, 'summary', self::summary_editor_options(), self::summary_editor_context(), self::summary_editor_filearea(), $data->id); - $DB->update_record('course_request', $data); - } + // Create a new course_request object and return it $request = new course_request($data); @@ -3750,32 +3731,12 @@ public static function summary_editor_options() { global $CFG; if (self::$summaryeditoroptions === null) { - self::$summaryeditoroptions = array('maxfiles' => 0, 'maxbytes'=>0, 'trusttext'=>true); + self::$summaryeditoroptions = array('maxfiles' => 0, 'maxbytes'=>0); } return self::$summaryeditoroptions; } /** - * Returns the context to use with the summary editor - * - * @uses course_request::$summaryeditorcontext - * @return stdClass The context to use - */ - public static function summary_editor_context() { - return null; - } - - /** - * Returns the filearea to use with the summary editor - * - * @uses course_request::$summaryeditorfilearea - * @return string The filearea to use with the summary editor - */ - public static function summary_editor_filearea() { - return self::$summaryeditorfilearea; - } - - /** * Loads the properties for this course request object. Id is required and if * only id is provided then we load the rest of the properties from the database * @@ -3810,9 +3771,6 @@ * @return mixed */ public function __get($key) { - if ($key === 'summary' && self::summary_editor_context() !== null) { - return file_rewrite_pluginfile_urls($this->properties->summary, 'pluginfile.php', self::summary_editor_context()->id, self::summary_editor_filearea(), $this->properties->id); - } return $this->properties->$key; } @@ -3927,13 +3885,12 @@ blocks_add_default_course_blocks($course); $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); // TODO: do some real enrolment here - role_assign($CFG->creatornewroleid, $this->properties->requester, $coursecontext->id); // assing teacher role + role_assign($CFG->creatornewroleid, $this->properties->requester, $coursecontext->id); // assign teacher role if (!empty($CFG->restrictmodulesfor) && $CFG->restrictmodulesfor != 'none' && !empty($CFG->restrictbydefault)) { // if we're all or requested we're ok. $allowedmods = explode(',',$CFG->defaultallowedmodules); update_restricted_mods($course, $allowedmods); } - $this->copy_summary_files_to_course($course); $this->delete(); fix_course_sortorder(); @@ -3967,36 +3924,6 @@ public function delete() { global $DB; $DB->delete_records('course_request', array('id' => $this->properties->id)); - if (self::summary_editor_context() !== null) { - $fs = get_file_storage(); - $files = $fs->get_area_files(self::summary_editor_context()->id, self::summary_editor_filearea(), $this->properties->id); - foreach ($files as $file) { - $file->delete(); - } - } - } - - /** - * This function copies all files used in the summary for the request to the - * summary of the course. - * - * This function copies, original files are left associated with the request - * and are removed only when the request is deleted - * - * @param stdClass $course An object representing the course to copy files to - */ - protected function copy_summary_files_to_course($course) { - if (self::summary_editor_context() !== null) { - $fs = get_file_storage(); - $files = $fs->get_area_files(self::summary_editor_context()->id, self::summary_editor_filearea(), $this->properties->id); - foreach ($files as $file) { - $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); - if (!$file->is_directory()) { - $filerecord = array('contextid'=>$coursecontext->id, 'filearea'=>'course_summary', 'itemid'=>0, 'filepath'=>$file->get_filepath(), 'filename'=>$file->get_filename()); - $fs->create_file_from_storedfile($filerecord, $file); - } - } - } } /** Index: course/category.php =================================================================== RCS file: /cvsroot/moodle/moodle/course/category.php,v retrieving revision 1.182 diff -u -r1.182 category.php --- course/category.php 21 Jun 2010 15:30:51 -0000 1.182 +++ course/category.php 28 Jun 2010 22:31:53 -0000 @@ -199,7 +199,7 @@ if (!isset($category->descriptionformat)) { $category->descriptionformat = FORMAT_MOODLE; } - $text = file_rewrite_pluginfile_urls($category->description, 'pluginfile.php', $context->id, 'category_description', $category->id); + $text = file_rewrite_pluginfile_urls($category->description, 'pluginfile.php', $context->id, 'coursecat', 'description', null); echo format_text($text, $category->descriptionformat, $options); echo $OUTPUT->box_end(); } Index: course/editsection.php =================================================================== RCS file: /cvsroot/moodle/moodle/course/editsection.php,v retrieving revision 1.48 diff -u -r1.48 editsection.php --- course/editsection.php 28 Jun 2010 17:05:05 -0000 1.48 +++ course/editsection.php 28 Jun 2010 22:31:53 -0000 @@ -40,7 +40,7 @@ require_capability('moodle/course:update', $context); $editoroptions = array('maxfiles' => EDITOR_UNLIMITED_FILES, 'maxbytes'=>$CFG->maxbytes, 'trusttext'=>false, 'noclean'=>true); -$section = file_prepare_standard_editor($section, 'summary', $editoroptions, $context, 'course_section', $section->id); +$section = file_prepare_standard_editor($section, 'summary', $editoroptions, $context, 'course', 'section', $section->id); $section->usedefaultname = (is_null($section->name)); $mform = new editsection_form(null, array('course'=>$course, 'editoroptions'=>$editoroptions)); $mform->set_data($section); // set current value @@ -55,7 +55,7 @@ } else { $section->name = null; } - $data = file_postupdate_standard_editor($data, 'summary', $editoroptions, $context, 'course_section', $section->id); + $data = file_postupdate_standard_editor($data, 'summary', $editoroptions, $context, 'course', 'section', $section->id); $section->summary = $data->summary; $section->summaryformat = $data->summaryformat; $DB->update_record('course_sections', $section); Index: course/info.php =================================================================== RCS file: /cvsroot/moodle/moodle/course/info.php,v retrieving revision 1.44 diff -u -r1.44 info.php --- course/info.php 21 Jun 2010 15:30:51 -0000 1.44 +++ course/info.php 28 Jun 2010 22:31:53 -0000 @@ -62,7 +62,7 @@ echo $OUTPUT->box_start('generalbox info'); - $course->summary = file_rewrite_pluginfile_urls($course->summary, 'pluginfile.php', $context->id, 'course_summary', NULL); + $course->summary = file_rewrite_pluginfile_urls($course->summary, 'pluginfile.php', $context->id, 'course', 'summary', NULL); echo format_text($course->summary, $course->summaryformat, NULL, $course->id); if (!empty($CFG->coursecontact)) { Index: mod/wiki/pagelib.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/wiki/pagelib.php,v retrieving revision 1.2 diff -u -r1.2 pagelib.php --- mod/wiki/pagelib.php 21 May 2010 11:07:44 -0000 1.2 +++ mod/wiki/pagelib.php 28 Jun 2010 22:33:02 -0000 @@ -532,17 +532,18 @@ switch ($format) { case 'html': $data->newcontentformat = FORMAT_HTML; - $data = file_prepare_standard_editor($data, 'newcontent', page_wiki_edit::$attachmentoptions, $context, 'wiki_attachments', $this->subwiki->id); + $data = file_prepare_standard_editor($data, 'newcontent', page_wiki_edit::$attachmentoptions, $context, 'mod_wiki', 'attachments', $this->subwiki->id); break; default: //$draftitemid = file_get_submitted_draft_itemid('attachments'); - //file_prepare_draft_area($draftitemid, $context->id, 'wiki_attachments', $this->subwiki->id); + //file_prepare_draft_area($draftitemid, $context->id, 'mod_wiki', 'attachments', $this->subwiki->id); //$data->attachments = $draftitemid; } if ($version->contentformat != 'html') { - $params['contextid'] = $context->id; - $params['filearea'] = 'wiki_attachments'; + $params['contextid'] = $context->id; + $params['component'] = 'mod_wiki'; + $params['filearea'] = 'attachments'; $params['fileitemid'] = $this->subwiki->id; } @@ -577,9 +578,9 @@ global $PAGE, $OUTPUT; if ($this->upload) { - file_save_draft_area_files($this->attachments, $context->id, 'wiki_attachments', $this->subwiki->id); + file_save_draft_area_files($this->attachments, $context->id, 'mod_wiki', 'attachments', $this->subwiki->id); return null; - //return wiki_process_attachments($this->attachments, $this->deleteuploads, $context->id, 'wiki_attachments', $this->subwiki->id); + //return wiki_process_attachments($this->attachments, $this->deleteuploads, $context->id, 'mod_wiki', 'attachments', $this->subwiki->id); } } } @@ -1909,8 +1910,9 @@ $params = array('attachmentoptions' => page_wiki_edit::$attachmentoptions, 'format' => $this->format, 'version' => $this->versionnumber); if ($this->format != 'html') { - $params['contextid'] = $context->id; - $params['filearea'] = 'wiki_attachments'; + $params['contextid'] = $context->id; + $params['component'] = 'mod_wiki'; + $params['filearea'] = 'attachments'; $params['fileitemid'] = $this->page->id; } @@ -1920,7 +1922,7 @@ $data = false; if ($data = $form->get_data()) { if ($this->format == 'html') { - $data = file_postupdate_standard_editor($data, 'newcontent', page_wiki_edit::$attachmentoptions, $context, 'wiki_attachments', $this->subwiki->id); + $data = file_postupdate_standard_editor($data, 'newcontent', page_wiki_edit::$attachmentoptions, $context, 'mod_wiki', 'attachments', $this->subwiki->id); } if (isset($this->section)) { Index: mod/wiki/renderer.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/wiki/renderer.php,v retrieving revision 1.5 diff -u -r1.5 renderer.php --- mod/wiki/renderer.php 21 May 2010 11:07:44 -0000 1.5 +++ mod/wiki/renderer.php 28 Jun 2010 22:33:03 -0000 @@ -63,7 +63,7 @@ $table->head = array('title' => $page->title . ' (' . html_writer::link($CFG->wwwroot . '/mod/wiki/view.php?pageid=' . $page->id, get_string('view', 'wiki')) . ')'); $table->align = array('title' => 'left'); $table->width = '100%'; - $table->data = array(array(file_rewrite_pluginfile_urls($page->cachedcontent, 'pluginfile.php', $context->id, 'wiki_attachments', $page->id))); + $table->data = array(array(file_rewrite_pluginfile_urls($page->cachedcontent, 'pluginfile.php', $context->id, 'mod_wiki', 'attachments', $page->id))); $table->colclasses = array('wikisearchresults'); $html .= html_writer::table($table); } Index: mod/wiki/edit_form.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/wiki/edit_form.php,v retrieving revision 1.2 diff -u -r1.2 edit_form.php --- mod/wiki/edit_form.php 13 May 2010 02:02:10 -0000 1.2 +++ mod/wiki/edit_form.php 28 Jun 2010 22:33:00 -0000 @@ -74,6 +74,7 @@ $mform->addElement('filemanager', 'attachments', get_string('attachments', 'wiki'), null, page_wiki_edit::$attachmentoptions); $fileinfo = array( 'contextid'=>$contextid, + 'component'=>'mod_wiki', 'filearea'=>$filearea, 'itemid'=>$fileitemid, ); Index: mod/wiki/lib.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/wiki/lib.php,v retrieving revision 1.85 diff -u -r1.85 lib.php --- mod/wiki/lib.php 1 Jun 2010 13:01:20 -0000 1.85 +++ mod/wiki/lib.php 28 Jun 2010 22:33:00 -0000 @@ -381,25 +381,29 @@ * * @author Josep Arus */ -function wiki_pluginfile($course, $cminfo, $context, $filearea, $args, $forcedownload) { +function wiki_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload) { global $CFG; + if ($context->contextlevel != CONTEXT_MODULE) { + return false; + } + + require_course_login($course, true, $cm); + require_once($CFG->dirroot . "/mod/wiki/locallib.php"); - if ($filearea == 'wiki_attachments') { + if ($filearea == 'attachments') { $swid = (int) array_shift($args); if (!$subwiki = wiki_get_subwiki($swid)) { return false; } - require_course_login($course->id, true, $cm); - require_capability('mod/wiki:viewpage', $context); - $relativepath = '/' . implode('/', $args); + $relativepath = implode('/', $args); - $fullpath = $context->id . 'wiki_attachments' . $swid . $relativepath; + $fullpath = "/$context->id/mod_wiki/attachments/$swid/$relativepath"; $fs = get_file_storage(); if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) { @@ -411,6 +415,7 @@ send_stored_file($file, $lifetime, 0); } } + function wiki_search_form($cm, $search='') { global $CFG, $OUTPUT; Index: mod/wiki/locallib.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/wiki/locallib.php,v retrieving revision 1.5 diff -u -r1.5 locallib.php --- mod/wiki/locallib.php 21 May 2010 11:07:44 -0000 1.5 +++ mod/wiki/locallib.php 28 Jun 2010 22:33:02 -0000 @@ -574,7 +574,7 @@ $cm = get_coursemodule_from_instance("wiki", $subwiki->wikiid); $context = get_context_instance(CONTEXT_MODULE, $cm->id); - $parser_options = array('link_callback' => '/mod/wiki/locallib.php:wiki_parser_link', 'link_callback_args' => array('swid' => $options['swid']), 'table_callback' => '/mod/wiki/locallib.php:wiki_parser_table', 'real_path_callback' => '/mod/wiki/locallib.php:wiki_parser_real_path', 'real_path_callback_args' => array('context' => $context, 'filearea' => 'wiki_attachments', 'pageid' => $options['pageid']), 'pageid' => $options['pageid'], 'pretty_print' => (isset($options['pretty_print']) && $options['pretty_print']), 'printable' => (isset($options['printable']) && $options['printable'])); + $parser_options = array('link_callback' => '/mod/wiki/locallib.php:wiki_parser_link', 'link_callback_args' => array('swid' => $options['swid']), 'table_callback' => '/mod/wiki/locallib.php:wiki_parser_table', 'real_path_callback' => '/mod/wiki/locallib.php:wiki_parser_real_path', 'real_path_callback_args' => array('context' => $context, 'component' => 'mod_wiki', 'filearea' => 'attachments', 'pageid' => $options['pageid']), 'pageid' => $options['pageid'], 'pretty_print' => (isset($options['pretty_print']) && $options['pretty_print']), 'printable' => (isset($options['printable']) && $options['printable'])); return wiki_parser_proxy::parse($pagecontent, $markup, $parser_options); } @@ -1016,7 +1016,7 @@ $usercontext = get_context_instance(CONTEXT_USER, $USER->id); $fs = get_file_storage(); - $oldfiles = $fs->get_area_files($contextid, $filearea, $itemid, 'id'); + $oldfiles = $fs->get_area_files($contextid, 'mod_wiki', 'attachments', $itemid, 'id'); foreach ($oldfiles as $file) { if (in_array($file->get_pathnamehash(), $deleteuploads)) { @@ -1024,14 +1024,14 @@ } } - $draftfiles = $fs->get_area_files($usercontext->id, 'user_draft', $draftitemid, 'id'); - $oldfiles = $fs->get_area_files($contextid, $filearea, $itemid, 'id'); + $draftfiles = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid, 'id'); + $oldfiles = $fs->get_area_files($contextid, 'mod_wiki', 'attachments', $itemid, 'id'); - $file_record = array('contextid' => $contextid, 'filearea' => $filearea, 'itemid' => $itemid); + $file_record = array('contextid' => $contextid, 'component'=>'mod_wiki', 'filearea' => 'attachments', 'itemid' => $itemid); //more or less a merge... $newhashes = array(); foreach ($draftfiles as $file) { - $newhash = sha1($contextid . $filearea . $itemid . $file->get_filepath() . $file->get_filename()); + $newhash = sha1("/$contextid/mod_wiki/attachments/$itemid" . $file->get_filepath() . $file->get_filename()); $newhashes[$newhash] = $file; } @@ -1065,7 +1065,7 @@ } //delete all draft files - $fs->delete_area_files($usercontext->id, 'user_draft', $draftitemid); + $fs->delete_area_files($usercontext->id, 'user', 'draft', $draftitemid); return $errors; } @@ -1215,7 +1215,7 @@ echo $OUTPUT->box($box); } } - $html = file_rewrite_pluginfile_urls($page->cachedcontent, 'pluginfile.php', $context->id, 'wiki_attachments', $subwikiid); + $html = file_rewrite_pluginfile_urls($page->cachedcontent, 'pluginfile.php', $context->id, 'mod_wiki', 'attachments', $subwikiid); echo $OUTPUT->box($html); if (!empty($CFG->usetags)) { @@ -1307,8 +1307,7 @@ $htmltable->head = array(get_string('deleteupload', 'wiki'), get_string('uploadname', 'wiki'), get_string('uploadactions', 'wiki')); $fs = get_file_storage(); - $browser = get_file_browser(); - $files = $fs->get_area_files($context->id, $filearea, $fileitemid); + $files = $fs->get_area_files($context->id, 'mod_wiki', $filearea, $fileitemid); //TODO: this is weird (skodak) foreach ($files as $file) { if (!$file->is_directory()) { Index: lib/file/file_info_course.php =================================================================== RCS file: lib/file/file_info_course.php diff -N lib/file/file_info_course.php --- lib/file/file_info_course.php 21 Jun 2010 15:31:02 -0000 1.10 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,125 +0,0 @@ -. - - -/** - * Utility class for browsing of course files. - * - * @package moodlecore - * @subpackage file-browser - * @copyright 2008 Petr Skoda (http://skodak.org) - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -/** - * Represents a course context in the tree navigated by @see{file_browser}. - */ -class file_info_course extends file_info { - protected $course; - - public function __construct($browser, $context, $course) { - global $DB; - parent::__construct($browser, $context); - $this->course = $course; - } - - /** - * Returns list of standard virtual file/directory identification. - * The difference from stored_file parameters is that null values - * are allowed in all fields - * @return array with keys contextid, filearea, itemid, filepath and filename - */ - public function get_params() { - return array('contextid'=>$this->context->id, - 'filearea' =>null, - 'itemid' =>null, - 'filepath' =>null, - 'filename' =>null); - } - - public function get_visible_name() { - return ($this->course->id == SITEID) ? get_string('frontpage', 'admin') : format_string($this->course->fullname); - } - - /** - * Can I add new files or directories? - * @return bool - */ - public function is_writable() { - return false; - } - - /** - * Is directory? - * @return bool - */ - public function is_directory() { - return true; - } - - /** - * Returns list of children. - * @return array of file_info instances - */ - public function get_children() { - $children = array(); - - if ($child = $this->browser->get_file_info($this->context, 'course_summary', 0)) { - $children[] = $child; - } - if ($child = $this->browser->get_file_info($this->context, 'course_section')) { - $children[] = $child; - } - if ($child = $this->browser->get_file_info($this->context, 'section_backup')) { - $children[] = $child; - } - - if ($child = $this->browser->get_file_info($this->context, 'course_backup', 0)) { - $children[] = $child; - } - - if ($this->course->legacyfiles == 2) { - if ($child = $this->browser->get_file_info($this->context, 'course_content', 0)) { - $children[] = $child; - } - } - - $modinfo = get_fast_modinfo($this->course); - foreach ($modinfo->cms as $cminfo) { - if (empty($cminfo->uservisible)) { - continue; - } - $modcontext = get_context_instance(CONTEXT_MODULE, $cminfo->id); - if ($child = $this->browser->get_file_info($modcontext)) { - $children[] = $child; - } - } - - return $children; - } - - /** - * Returns parent file_info instance - * @return file_info or null for root - */ - public function get_parent() { - //TODO: error checking if get_parent_contextid() returns false - $pcid = get_parent_contextid($this->context); - $parent = get_context_instance_by_id($pcid); - return $this->browser->get_file_info($parent); - } -} Index: lib/file/file_info_coursefile.php =================================================================== RCS file: lib/file/file_info_coursefile.php diff -N lib/file/file_info_coursefile.php --- lib/file/file_info_coursefile.php 21 May 2009 09:37:21 -0000 1.10 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,83 +0,0 @@ -. - - -/** - * Utility class for browsing of coursefiles. - * - * @package moodlecore - * @subpackage file-browser - * @copyright 2008 Petr Skoda (http://skodak.org) - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -/** - * Subclass of file_info_stored for files in the course files area. - */ -class file_info_coursefile extends file_info_stored { - public function __construct($browser, $context, $storedfile) { - global $CFG; - $urlbase = $CFG->wwwroot.'/file.php'; - parent::__construct($browser, $context, $storedfile, $urlbase, get_string('coursefiles'), false, true, true, false); - } - - /** - * Returns file download url - * @param bool $forcedownload - * @param bool $htts force https - * @return string url - */ - public function get_url($forcedownload=false, $https=false) { - global $CFG; - - if (!$this->is_readable()) { - return null; - } - - if ($this->lf->is_directory()) { - return null; - } - - $filepath = $this->lf->get_filepath(); - $filename = $this->lf->get_filename(); - $courseid = $this->context->instanceid; - - $path = '/'.$courseid.$filepath.$filename; - - return file_encode_url($this->urlbase, $path, $forcedownload, $https); - } - - /** - * Returns list of children. - * @return array of file_info instances - */ - public function get_children() { - if (!$this->lf->is_directory()) { - return array(); - } - - $result = array(); - $fs = get_file_storage(); - - $storedfiles = $fs->get_directory_files($this->context->id, 'course_content', 0, $this->lf->get_filepath(), false, true, "filepath, filename"); - foreach ($storedfiles as $file) { - $result[] = new file_info_coursefile($this->browser, $this->context, $file); - } - - return $result; - } -} Index: lib/file/file_info_coursesectionbackup.php =================================================================== RCS file: lib/file/file_info_coursesectionbackup.php diff -N lib/file/file_info_coursesectionbackup.php --- lib/file/file_info_coursesectionbackup.php 3 May 2010 05:19:38 -0000 1.1 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,106 +0,0 @@ -. - - -/** - * Utility class for browsing of course section files. - * - * @package moodlecore - * @subpackage file-browser - * @copyright 2010 Dongsheng Cai - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -/** - */ -class file_info_coursesectionbackup extends file_info { - protected $course; - - public function __construct($browser, $context, $course) { - parent::__construct($browser, $context); - $this->course = $course; - } - - /** - * Returns list of standard virtual file/directory identification. - * The difference from stored_file parameters is that null values - * are allowed in all fields - * @return array with keys contextid, filearea, itemid, filepath and filename - */ - public function get_params() { - return array('contextid'=>$this->context->id, - 'filearea' =>'section_backup', - 'itemid' =>null, - 'filepath' =>null, - 'filename' =>null); - } - - /** - * Returns localised visible name. - * @return string - */ - public function get_visible_name() { - $format = $this->course->format; - $sectionsname = get_string('sectionbackup', 'repository'); - - return $sectionsname; - } - - /** - * Can I add new files or directories? - * @return bool - */ - public function is_writable() { - return false; - } - - /** - * Is directory? - * @return bool - */ - public function is_directory() { - return true; - } - - /** - * Returns list of children. - * @return array of file_info instances - */ - public function get_children() { - global $DB; - - $children = array(); - - $course_sections = $DB->get_records('course_sections', array('course'=>$this->course->id), 'section'); - foreach ($course_sections as $section) { - if ($child = $this->browser->get_file_info($this->context, 'section_backup', $section->id)) { - $children[] = $child; - } - } - - return $children; - } - - /** - * Returns parent file_info instance - * @return file_info or null for root - */ - public function get_parent() { - return $this->browser->get_file_info($this->context); - } -} - Index: lib/file/file_info_stored.php =================================================================== RCS file: lib/file/file_info_stored.php diff -N lib/file/file_info_stored.php --- lib/file/file_info_stored.php 1 Jun 2010 08:20:01 -0000 1.18 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,491 +0,0 @@ -. - - -/** - * Utility class for browsing of stored files. - * - * @package moodlecore - * @subpackage file-browser - * @copyright 2008 Petr Skoda (http://skodak.org) - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -/** - * Represents an actual file or folder - a row in the file table - - * in the tree navigated by @see{file_browser}. - */ -class file_info_stored extends file_info { - protected $lf; - protected $urlbase; - protected $topvisiblename; - protected $itemidused; - protected $readaccess; - protected $writeaccess; - protected $areaonly; - - public function __construct($browser, $context, $storedfile, $urlbase, $topvisiblename, $itemidused, $readaccess, $writeaccess, $areaonly) { - parent::__construct($browser, $context); - - $this->lf = $storedfile; - $this->urlbase = $urlbase; - $this->topvisiblename = $topvisiblename; - $this->itemidused = $itemidused; - $this->readaccess = $readaccess; - $this->writeaccess = $writeaccess; - $this->areaonly = $areaonly; - } - - /** - * Returns list of standard virtual file/directory identification. - * The difference from stored_file parameters is that null values - * are allowed in all fields - * @return array with keys contextid, filearea, itemid, filepath and filename - */ - public function get_params() { - return array('contextid'=>$this->context->id, - 'filearea' =>$this->lf->get_filearea(), - 'itemid' =>$this->lf->get_itemid(), - 'filepath' =>$this->lf->get_filepath(), - 'filename' =>$this->lf->get_filename()); - } - - /** - * Returns localised visible name. - * @return string - */ - public function get_visible_name() { - $filename = $this->lf->get_filename(); - $filepath = $this->lf->get_filepath(); - - if ($filename !== '.') { - return $filename; - - } else { - $dir = trim($filepath, '/'); - $dir = explode('/', $dir); - $dir = array_pop($dir); - if ($dir === '') { - return $this->topvisiblename; - } else { - return $dir; - } - } - } - - /** - * Returns file download url - * @param bool $forcedownload - * @param bool $htts force https - * @return string url - */ - public function get_url($forcedownload=false, $https=false) { - global $CFG; - - if (!$this->is_readable()) { - return null; - } - - if ($this->is_directory()) { - return null; - } - - $this->urlbase; - $contextid = $this->lf->get_contextid(); - $filearea = $this->lf->get_filearea(); - $filepath = $this->lf->get_filepath(); - $filename = $this->lf->get_filename(); - $itemid = $this->lf->get_itemid(); - - if ($this->itemidused) { - $path = '/'.$contextid.'/'.$filearea.'/'.$itemid.$filepath.$filename; - } else { - $path = '/'.$contextid.'/'.$filearea.$filepath.$filename; - } - return file_encode_url($this->urlbase, $path, $forcedownload, $https); - } - - /** - * Can I read content of this file or enter directory? - * @return bool - */ - public function is_readable() { - return $this->readaccess; - } - - /** - * Can I add new files or directories? - * @return bool - */ - public function is_writable() { - return $this->writeaccess; - } - - /** - * Returns file size in bytes, null for directories - * @return int bytes or null if not known - */ - public function get_filesize() { - return $this->lf->get_filesize(); - } - - /** - * Returns mimetype - * @return string mimetype or null if not known - */ - public function get_mimetype() { - return $this->lf->get_mimetype(); - } - - /** - * Returns time created unix timestamp if known - * @return int timestamp or null - */ - public function get_timecreated() { - return $this->lf->get_timecreated(); - } - - /** - * Returns time modified unix timestamp if known - * @return int timestamp or null - */ - public function get_timemodified() { - return $this->lf->get_timemodified(); - } - - /** - * Is directory? - * @return bool - */ - public function is_directory() { - return $this->lf->is_directory(); - } - - /** - * Returns the license type of the file - * @return string license short name or null - */ - public function get_license() { - return $this->lf->get_license(); - } - - /** - * Returns the author name of the file - * @return string author name or null - */ - public function get_author() { - return $this->lf->get_author(); - } - - /** - * Returns the source of the file - * @return string a source url or null - */ - public function get_source() { - return $this->lf->get_source(); - } - - /** - * Returns the sort order of the file - * @return int - */ - public function get_sortorder() { - return $this->lf->get_sortorder(); - } - - /** - * Returns list of children. - * @return array of file_info instances - */ - public function get_children() { - if (!$this->lf->is_directory()) { - return array(); - } - - $result = array(); - $fs = get_file_storage(); - - $storedfiles = $fs->get_directory_files($this->context->id, $this->lf->get_filearea(), $this->lf->get_itemid(), - $this->lf->get_filepath(), false, true, "filepath, filename"); - foreach ($storedfiles as $file) { - $result[] = new file_info_stored($this->browser, $this->context, $file, $this->urlbase, $this->topvisiblename, - $this->itemidused, $this->readaccess, $this->writeaccess, false); - } - - return $result; - } - - /** - * Returns parent file_info instance - * @return file_info or null for root - */ - public function get_parent() { - if ($this->lf->get_filepath() === '/' and $this->lf->is_directory()) { - if ($this->areaonly) { - return null; - } else if ($this->itemidused) { - return $this->browser->get_file_info($this->context, $this->lf->get_filearea()); - } else { - return $this->browser->get_file_info($this->context); - } - } - - if (!$this->lf->is_directory()) { - return $this->browser->get_file_info($this->context, $this->lf->get_filearea(), $this->lf->get_itemid(), $this->lf->get_filepath(), '.'); - } - - $filepath = $this->lf->get_filepath(); - $filepath = trim($filepath, '/'); - $dirs = explode('/', $filepath); - array_pop($dirs); - $filepath = implode('/', $dirs); - $filepath = ($filepath === '') ? '/' : "/$filepath/"; - - return $this->browser->get_file_info($this->context, $this->lf->get_filearea(), $this->lf->get_itemid(), $filepath, '.'); - } - - /** - * Create new directory, may throw exception - make sure - * params are valid. - * @param string $newdirname name of new directory - * @param int id of author, default $USER->id - * @return file_info new directory - */ - public function create_directory($newdirname, $userid=null) { - if (!$this->is_writable() or !$this->lf->is_directory()) { - return null; - } - - $newdirname = clean_param($newdirname, PARAM_FILE); - if ($newdirname === '') { - return null; - } - - $filepath = $this->lf->get_filepath().'/'.$newdirname.'/'; - - $fs = get_file_storage(); - - if ($file = $fs->create_directory($this->lf->get_contextid(), $this->lf->get_filearea(), $this->lf->get_itemid(), $filepath, $userid)) { - return $this->browser->get_file_info($this->context, $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $file->get_filename()); - } - return null; - } - - - /** - * Create new file from string - make sure - * params are valid. - * @param string $newfilename name of new file - * @param string $content of file - * @param int id of author, default $USER->id - * @return file_info new file - */ - public function create_file_from_string($newfilename, $content, $userid=null) { - if (!$this->is_writable() or !$this->lf->is_directory()) { - return null; - } - - $newfilename = clean_param($newfilename, PARAM_FILE); - if ($newfilename === '') { - return null; - } - - $fs = get_file_storage(); - - $now = time(); - - $newrecord = new object(); - $newrecord->contextid = $this->lf->get_contextid(); - $newrecord->filearea = $this->lf->get_filearea(); - $newrecord->itemid = $this->lf->get_itemid(); - $newrecord->filepath = $this->lf->get_filepath(); - $newrecord->filename = $newfilename; - - if ($fs->file_exists($newrecord->contextid, $newrecord->filearea, $newrecord->itemid, $newrecord->filepath, $newrecord->filename)) { - // file already exists, sorry - return null; - } - - $newrecord->timecreated = $now; - $newrecord->timemodified = $now; - $newrecord->mimetype = mimeinfo('type', $newfilename); - $newrecord->userid = $userid; - - if ($file = $fs->create_file_from_string($newrecord, $content)) { - return $this->browser->get_file_info($this->context, $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $file->get_filename()); - } - return null; - } - - /** - * Create new file from pathname - make sure - * params are valid. - * @param string $newfilename name of new file - * @param string $pathname location of file - * @param int id of author, default $USER->id - * @return file_info new file - */ - public function create_file_from_pathname($newfilename, $pathname, $userid=null) { - if (!$this->is_writable() or !$this->lf->is_directory()) { - return null; - } - - $newfilename = clean_param($newfilename, PARAM_FILE); - if ($newfilename === '') { - return null; - } - - $fs = get_file_storage(); - - $now = time(); - - $newrecord = new object(); - $newrecord->contextid = $this->lf->get_contextid(); - $newrecord->filearea = $this->lf->get_filearea(); - $newrecord->itemid = $this->lf->get_itemid(); - $newrecord->filepath = $this->lf->get_filepath(); - $newrecord->filename = $newfilename; - - if ($fs->file_exists($newrecord->contextid, $newrecord->filearea, $newrecord->itemid, $newrecord->filepath, $newrecord->filename)) { - // file already exists, sorry - return null; - } - - $newrecord->timecreated = $now; - $newrecord->timemodified = $now; - $newrecord->mimetype = mimeinfo('type', $newfilename); - $newrecord->userid = $userid; - - if ($file = $fs->create_file_from_pathname($newrecord, $pathname)) { - return $this->browser->get_file_info($this->context, $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $file->get_filename()); - } - return null; - } - - /** - * Create new file from stored file - make sure - * params are valid. - * @param string $newfilename name of new file - * @param mixed dile id or stored_file of file - * @param int id of author, default $USER->id - * @return file_info new file - */ - public function create_file_from_storedfile($newfilename, $fid, $userid=null) { - if (!$this->is_writable() or $this->lf->get_filename() !== '.') { - return null; - } - - $newfilename = clean_param($newfilename, PARAM_FILE); - if ($newfilename === '') { - return null; - } - - $fs = get_file_storage(); - - $now = time(); - - $newrecord = new object(); - $newrecord->contextid = $this->lf->get_contextid(); - $newrecord->filearea = $this->lf->get_filearea(); - $newrecord->itemid = $this->lf->get_itemid(); - $newrecord->filepath = $this->lf->get_filepath(); - $newrecord->filename = $newfilename; - - if ($fs->file_exists($newrecord->contextid, $newrecord->filearea, $newrecord->itemid, $newrecord->filepath, $newrecord->filename)) { - // file already exists, sorry - return null; - } - - $newrecord->timecreated = $now; - $newrecord->timemodified = $now; - $newrecord->mimetype = mimeinfo('type', $newfilename); - $newrecord->userid = $userid; - - if ($file = $fs->create_file_from_storedfile($newrecord, $fid)) { - return $this->browser->get_file_info($this->context, $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $file->get_filename()); - } - return null; - } - - /** - * Delete file, make sure file is deletable first. - * @return bool success - */ - public function delete() { - if (!$this->is_writable()) { - return false; - } - - if ($this->is_directory()) { - $filepath = $this->lf->get_filepath(); - $fs = get_file_storage(); - $storedfiles = $fs->get_area_files($this->context->id, $this->lf->get_filearea(), $this->lf->get_itemid(), ""); - foreach ($storedfiles as $file) { - if (strpos($file->get_filepath(), $filepath) === 0) { - $file->delete(); - } - } - } - - return $this->lf->delete(); - } - - /** - * Copy content of this file to local storage, overriding current file if needed. - * @param int $contextid - * @param string $filearea - * @param int $itemid - * @param string $filepath - * @param string $filename - * @return boolean success - */ - public function copy_to_storage($contextid, $filearea, $itemid, $filepath, $filename) { - if (!$this->is_readable() or $this->is_directory()) { - return false; - } - - $fs = get_file_storage(); - if ($existing = $fs->get_file($contextid, $filearea, $itemid, $filepath, $filename)) { - $existing->delete(); - } - $file_record = array('contextid'=>$contextid, 'filearea'=>$filearea, 'itemid'=>$itemid, 'filepath'=>$filepath, 'filename'=>$filename); - $fs->create_file_from_storedfile($file_record, $this->lf); - - return true; - } - - /** - * Copy content of this file to local storage, overriding current file if needed. - * @param string $pathname real local full file name - * @return boolean success - */ - public function copy_to_pathname($pathname) { - if (!$this->is_readable() or $this->is_directory()) { - return false; - } - - if (file_exists($pathname)) { - if (!unlink($pathname)) { - return false; - } - } - - $this->lf->copy_content_to($pathname); - - return true; - } -} Index: lib/file/file_info_coursesection.php =================================================================== RCS file: lib/file/file_info_coursesection.php diff -N lib/file/file_info_coursesection.php --- lib/file/file_info_coursesection.php 21 May 2009 09:37:21 -0000 1.4 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,106 +0,0 @@ -. - - -/** - * Utility class for browsing of course section files. - * - * @package moodlecore - * @subpackage file-browser - * @copyright 2008 Petr Skoda (http://skodak.org) - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -/** - * Represents a course category context in the tree navigated by @see{file_browser}. - */ -class file_info_coursesection extends file_info { - protected $course; - - public function __construct($browser, $context, $course) { - parent::__construct($browser, $context); - $this->course = $course; - } - - /** - * Returns list of standard virtual file/directory identification. - * The difference from stored_file parameters is that null values - * are allowed in all fields - * @return array with keys contextid, filearea, itemid, filepath and filename - */ - public function get_params() { - return array('contextid'=>$this->context->id, - 'filearea' =>'course_section', - 'itemid' =>null, - 'filepath' =>null, - 'filename' =>null); - } - - /** - * Returns localised visible name. - * @return string - */ - public function get_visible_name() { - $format = $this->course->format; - $sectionsname = get_string("coursesectionsummaries"); - - return $sectionsname; - } - - /** - * Can I add new files or directories? - * @return bool - */ - public function is_writable() { - return false; - } - - /** - * Is directory? - * @return bool - */ - public function is_directory() { - return true; - } - - /** - * Returns list of children. - * @return array of file_info instances - */ - public function get_children() { - global $DB; - - $children = array(); - - $course_sections = $DB->get_records('course_sections', array('course'=>$this->course->id), 'section'); - foreach ($course_sections as $section) { - if ($child = $this->browser->get_file_info($this->context, 'course_section', $section->id)) { - $children[] = $child; - } - } - - return $children; - } - - /** - * Returns parent file_info instance - * @return file_info or null for root - */ - public function get_parent() { - return $this->browser->get_file_info($this->context); - } -} Index: lib/file/file_info_module.php =================================================================== RCS file: lib/file/file_info_module.php diff -N lib/file/file_info_module.php --- lib/file/file_info_module.php 27 Jul 2009 19:32:45 -0000 1.6 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,105 +0,0 @@ -. - - -/** - * Utility class for browsing of module files. - * - * @package moodlecore - * @subpackage file-browser - * @copyright 2008 Petr Skoda (http://skodak.org) - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -/** - * Represents a module context in the tree navigated by @see{file_browser}. - */ -class file_info_module extends file_info { - protected $course; - protected $cm; - protected $areas; - - public function __construct($browser, $course, $cm, $context, $areas) { - global $DB; - parent::__construct($browser, $context); - $this->course = $course; - $this->cm = $cm; - $this->areas = $areas; - } - - /** - * Returns list of standard virtual file/directory identification. - * The difference from stored_file parameters is that null values - * are allowed in all fields - * @return array with keys contextid, filearea, itemid, filepath and filename - */ - public function get_params() { - return array('contextid'=>$this->context->id, - 'filearea' =>null, - 'itemid' =>null, - 'filepath' =>null, - 'filename' =>null); - } - - /** - * Returns localised visible name. - * @return string - */ - public function get_visible_name() { - return $this->cm->name.' ('.get_string('modulename', $this->cm->modname).')'; - } - - /** - * Can I add new files or directories? - * @return bool - */ - public function is_writable() { - return false; - } - - /** - * Is directory? - * @return bool - */ - public function is_directory() { - return true; - } - - /** - * Returns list of children. - * @return array of file_info instances - */ - public function get_children() { - $children = array(); - foreach ($this->areas as $area=>$desctiption) { - if ($child = $this->browser->get_file_info($this->context, $area, null)) { - $children[] = $child; - } - } - return $children; - } - - /** - * Returns parent file_info instance - * @return file_info or null for root - */ - public function get_parent() { - $pcid = get_parent_contextid($this->context); - $parent = get_context_instance_by_id($pcid); - return $this->browser->get_file_info($parent); - } -} Index: lib/file/virtual_root_file.php =================================================================== RCS file: lib/file/virtual_root_file.php diff -N lib/file/virtual_root_file.php --- lib/file/virtual_root_file.php 21 May 2010 18:50:07 -0000 1.6 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,212 +0,0 @@ -. - - -/** - * Class simulating empty directories. - * - * @package moodlecore - * @subpackage file-browser - * @copyright 2008 Petr Skoda (http://skodak.org) - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -/** - * Represents the root directory of an empty file area in the tree navigated by - * @see{file_browser}. - */ -class virtual_root_file { - protected $contextid; - protected $filearea; - protected $itemid; - - /** - * Constructor - */ - public function __construct($contextid, $filearea, $itemid) { - $this->contextid = $contextid; - $this->filearea = $filearea; - $this->itemid = $itemid; - } - - /** - * Is this a directory? - * @return bool - */ - public function is_directory() { - return true; - } - - /** - * Delete file - * @return success - */ - public function delete() { - return true; - } - - /** - * adds this file path to a curl request (POST only) - * - * @param curl $curlrequest the curl request object - * @param string $key what key to use in the POST request - */ - public function add_to_curl_request(&$curlrequest, $key) { - return; - } - - /** - * Returns file handle - read only mode, no writing allowed into pool files! - * @return file handle - */ - public function get_content_file_handle() { - return null; - } - - /** - * Dumps file content to page - * @return file handle - */ - public function readfile() { - return; - } - - /** - * Returns file content as string - * @return string content - */ - public function get_content() { - return ''; - } - - /** - * Copy content of file to given pathname - * @param string $pathname real path to new file - * @return bool success - */ - public function copy_content_to($pathname) { - return false; - } - - /** - * List contents of archive - * @param object $file_packer - * @return array of file infos - */ - public function list_files(file_packer $packer) { - return null; - } - - /** - * Extract file to given file path (real OS filesystem), existing files are overwrited - * @param object $file_packer - * @param string $pathname target directory - * @return mixed list of processed files; false if error - */ - public function extract_to_pathname(file_packer $packer, $pathname) { - return false; - } - - /** - * Extract file to given file path (real OS filesystem), existing files are overwrited - * @param object $file_packer - * @param int $contextid - * @param string $filearea - * @param int $itemid - * @param string $pathbase - * @param int $userid - * @return mixed list of processed files; false if error - */ - public function extract_to_storage(file_packer $packer, $contextid, $filearea, $itemid, $pathbase, $userid=null) { - return false; - } - - /** - * Add file/directory into archive - * @param object $filearch - * @param string $archivepath pathname in archive - * @return bool success - */ - public function archive_file(file_archive $filearch, $archivepath) { - return false; - } - - /** - * Returns parent directory - * @return object stored_file - */ - public function get_parent_directory() { - return null; - } - - public function get_contextid() { - return $this->contextid; - } - - public function get_filearea() { - return $this->filearea; - } - - public function get_itemid() { - return $this->itemid; - } - - public function get_filepath() { - return '/'; - } - - public function get_filename() { - return '.'; - } - - public function get_userid() { - return null; - } - - public function get_filesize() { - return 0; - } - - public function get_mimetype() { - return null; - } - - public function get_timecreated() { - return 0; - } - - public function get_timemodified() { - return 0; - } - - public function get_status() { - return 0; - } - - public function get_id() { - return 0; - } - - public function get_contenthash() { - return sha1(''); - } - - public function get_pathnamehash() { - return sha1($this->get_contextid().$this->get_filearea().$this->get_itemid().$this->get_filepath().$this->get_filename()); - } - -} Index: lib/file/file_info_coursecat.php =================================================================== RCS file: lib/file/file_info_coursecat.php diff -N lib/file/file_info_coursecat.php --- lib/file/file_info_coursecat.php 21 May 2009 09:37:21 -0000 1.5 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,124 +0,0 @@ -. - - -/** - * Utility class for browsing of curse category files. - * - * @package moodlecore - * @subpackage file-browser - * @copyright 2008 Petr Skoda (http://skodak.org) - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -/** - * Represents a course category context in the tree navigated by @see{file_browser}. - */ -class file_info_coursecat extends file_info { - protected $category; - - public function __construct($browser, $context, $category) { - parent::__construct($browser, $context); - $this->category = $category; - } - - /** - * Returns list of standard virtual file/directory identification. - * The difference from stored_file parameters is that null values - * are allowed in all fields - * @return array with keys contextid, filearea, itemid, filepath and filename - */ - public function get_params() { - return array('contextid'=>$this->context->id, - 'filearea' =>null, - 'itemid' =>null, - 'filepath' =>null, - 'filename' =>null); - } - - /** - * Returns localised visible name. - * @return string - */ - public function get_visible_name() { - return format_string($this->category->name); - } - - /** - * Can I add new files or directories? - * @return bool - */ - public function is_writable() { - return false; - } - - /** - * Is directory? - * @return bool - */ - public function is_directory() { - return true; - } - - /** - * Returns list of children. - * @return array of file_info instances - */ - public function get_children() { - global $DB; - - $children = array(); - - if ($child = $this->browser->get_file_info($this->context, 'coursecat_intro', 0)) { - $children[] = $child; - } - - $course_cats = $DB->get_records('course_categories', array('parent'=>$this->category->id), 'sortorder'); - foreach ($course_cats as $category) { - $context = get_context_instance(CONTEXT_COURSECAT, $category->id); - if (!$category->visible and !has_capability('moodle/course:viewhiddencourses', $context)) { - continue; - } - if ($child = $this->browser->get_file_info($context)) { - $children[] = $child; - } - } - - $courses = $DB->get_records('course', array('category'=>$this->category->id), 'sortorder'); - foreach ($courses as $course) { - $context = get_context_instance(CONTEXT_COURSE, $course->id); - if (!$course->visible and !has_capability('moodle/course:viewhiddencourses', $context)) { - continue; - } - if ($child = $this->browser->get_file_info($context)) { - $children[] = $child; - } - } - - return $children; - } - - /** - * Returns parent file_info instance - * @return file_info or null for root - */ - public function get_parent() { - $cid = get_parent_contextid($this->context); - $parent = get_context_instance_by_id($cid); - return $this->browser->get_file_info($parent); - } -} Index: lib/file/file_exceptions.php =================================================================== RCS file: lib/file/file_exceptions.php diff -N lib/file/file_exceptions.php --- lib/file/file_exceptions.php 21 May 2010 18:50:08 -0000 1.4 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,67 +0,0 @@ -. - -/** - * File handling related exceptions. - * - * @package moodlecore - * @subpackage file - * @copyright 2008 Petr Skoda (http://skodak.org) - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -/** - * Basic file related exception class - */ -class file_exception extends moodle_exception { - function __construct($errorcode, $a=NULL, $debuginfo=null) { - parent::__construct($errorcode, '', '', $a, $debuginfo); - } -} - -/** - * Can not create file exception - */ -class stored_file_creation_exception extends file_exception { - function __construct($contextid, $filearea, $itemid, $filepath, $filename, $debuginfo=null) { - $a = new object(); - $a->contextid = $contextid; - $a->filearea = $filearea; - $a->itemid = $itemid; - $a->filepath = $filepath; - $a->filename = $filename; - parent::__construct('storedfilenotcreated', $a, $debuginfo); - } -} - -/** - * No file access exception. - */ -class file_access_exception extends file_exception { - function __construct($debuginfo=null) { - parent::__construct('nopermissions', NULL, $debuginfo); - } -} - -/** - * Hash file content problem exception. - */ -class file_pool_content_exception extends file_exception { - function __construct($contenthash, $debuginfo=null) { - parent::__construct('hashpoolproblem', $contenthash, $debuginfo); - } -} Index: lib/file/file_types.mm =================================================================== RCS file: lib/file/file_types.mm diff -N lib/file/file_types.mm --- lib/file/file_types.mm 8 Dec 2008 05:19:10 -0000 1.1 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,101 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Index: lib/file/file_info_user.php =================================================================== RCS file: lib/file/file_info_user.php diff -N lib/file/file_info_user.php --- lib/file/file_info_user.php 18 May 2010 01:49:50 -0000 1.9 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,119 +0,0 @@ -. - - -/** - * Utility class for browsing of user files. - * - * @package moodlecore - * @subpackage file-browser - * @copyright 2008 Petr Skoda (http://skodak.org) - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -/** - * Represents a user context in the tree navigated by @see{file_browser}. - */ -class file_info_user extends file_info { - protected $user; - - public function __construct($browser, $context) { - global $DB, $USER; - - parent::__construct($browser, $context); - - $userid = $context->instanceid; - - if ($userid == $USER->id) { - $this->user = $USER; - } else { - // if context exists user record should exist too ;-) - $this->user = $DB->get_record('user', array('id'=>$userid)); - } - } - - /** - * Returns list of standard virtual file/directory identification. - * The difference from stored_file parameters is that null values - * are allowed in all fields - * @return array with keys contextid, filearea, itemid, filepath and filename - */ - public function get_params() { - return array('contextid'=>$this->context->id, - 'filearea' =>null, - 'itemid' =>null, - 'filepath' =>null, - 'filename' =>null); - } - - /** - * Returns localised visible name. - * @return string - */ - public function get_visible_name() { - return fullname($this->user, true); - } - - /** - * Can I add new files or directories? - * @return bool - */ - public function is_writable() { - return false; - } - - /** - * Is directory? - * @return bool - */ - public function is_directory() { - return true; - } - - /** - * Returns list of children. - * @return array of file_info instances - */ - public function get_children() { - global $USER, $CFG; - - $children = array(); - - if ($child = $this->browser->get_file_info(get_context_instance(CONTEXT_USER, $USER->id), 'user_private', 0)) { - $children[] = $child; - } - - if ($child = $this->browser->get_file_info(get_context_instance(CONTEXT_USER, $USER->id), 'user_profile', 0)) { - $children[] = $child; - } - - if ($child = $this->browser->get_file_info(get_context_instance(CONTEXT_USER, $USER->id), 'user_backup', 0)) { - $children[] = $child; - } - // do not list user_draft here - it is browsable only if you know the draft itemid ;-) - - return $children; - } - - /** - * Returns parent file_info instance - * @return file_info or null for root - */ - public function get_parent() { - return $this->browser->get_file_info(get_context_instance(CONTEXT_SYSTEM)); - } -} Index: lib/file/file_info.php =================================================================== RCS file: lib/file/file_info.php diff -N lib/file/file_info.php --- lib/file/file_info.php 28 May 2010 07:29:12 -0000 1.9 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,263 +0,0 @@ -. - - -/** - * Base for all file browsing classes. - * - * @package moodlecore - * @subpackage file-browser - * @copyright 2008 Petr Skoda (http://skodak.org) - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -/** - * Base class for things in the tree navigated by @see{file_browser}. - */ -abstract class file_info { - - protected $context; - - protected $browser; - - public function __construct($browser, $context) { - $this->browser = $browser; - $this->context = $context; - } - - /** - * Returns list of standard virtual file/directory identification. - * The difference from stored_file parameters is that null values - * are allowed in all fields - * @return array with keys contextid, filearea, itemid, filepath and filename - */ - public abstract function get_params(); - - /** - * Returns localised visible name. - * @return string - */ - public abstract function get_visible_name(); - - /** - * Is directory? - * @return bool - */ - public abstract function is_directory(); - - /** - * Returns list of children. - * @return array of file_info instances - */ - public abstract function get_children(); - - /** - * Returns parent file_info instance - * @return file_info or null for root - */ - public abstract function get_parent(); - - /** - * Returns array of url encoded params. - * @return array with numeric keys - */ - public function get_params_rawencoded() { - $params = $this->get_params(); - $encoded = array(); - $encoded[] = 'contextid='.$params['contextid']; - $encoded[] = 'filearea='.$params['filearea']; - $encoded[] = 'itemid='.(is_null($params['itemid']) ? -1 : $params['itemid']); - $encoded[] = 'filepath='.(is_null($params['filepath']) ? '' : rawurlencode($params['filepath'])); - $encoded[] = 'filename='.((is_null($params['filename']) or $params['filename'] === '.') ? '' : rawurlencode($params['filename'])); - - return $encoded; - } - - /** - * Returns file download url - * @param bool $forcedownload - * @param bool $htts force https - * @return string url - */ - public function get_url($forcedownload=false, $https=false) { - return null; - } - - /** - * Can I read content of this file or enter directory? - * @return bool - */ - public function is_readable() { - return true; - } - - /** - * Can I add new files or directories? - * @return bool - */ - public function is_writable() { - return true; - } - - /** - * Returns file size in bytes, null for directories - * @return int bytes or null if not known - */ - public function get_filesize() { - return null; - } - - /** - * Returns mimetype - * @return string mimetype or null if not known - */ - public function get_mimetype() { - return null; - } - - /** - * Returns time created unix timestamp if known - * @return int timestamp or null - */ - public function get_timecreated() { - return null; - } - - /** - * Returns time modified unix timestamp if known - * @return int timestamp or null - */ - public function get_timemodified() { - return null; - } - - /** - * Returns the license type of the file - * @return string license short name or null - */ - public function get_license() { - return null; - } - - /** - * Returns the author name of the file - * @return string author name or null - */ - public function get_author() { - return null; - } - - /** - * Returns the source of the file - * @return string a source url or null - */ - public function get_source() { - return null; - } - - /** - * Returns the sort order of the file - * @return int - */ - public function get_sortorder() { - return 0; - } - - /** - * Create new directory, may throw exception - make sure - * params are valid. - * @param string $newdirname name of new directory - * @param int id of author, default $USER->id - * @return file_info new directory - */ - public function create_directory($newdirname, $userid=null) { - return null; - } - - /** - * Create new file from string - make sure - * params are valid. - * @param string $newfilename name of new file - * @param string $content of file - * @param int id of author, default $USER->id - * @return file_info new file - */ - public function create_file_from_string($newfilename, $content, $userid=null) { - return null; - } - - /** - * Create new file from pathname - make sure - * params are valid. - * @param string $newfilename name of new file - * @param string $pathname location of file - * @param int id of author, default $USER->id - * @return file_info new file - */ - public function create_file_from_pathname($newfilename, $pathname, $userid=null) { - return null; - } - - /** - * Create new file from stored file - make sure - * params are valid. - * @param string $newfilename name of new file - * @param mixed dile id or stored_file of file - * @param int id of author, default $USER->id - * @return file_info new file - */ - public function create_file_from_storedfile($newfilename, $fid, $userid=null) { - return null; - } - - /** - * Delete file, make sure file is deletable first. - * @return bool success - */ - public function delete() { - return false; - } - - /** - * Copy content of this file to local storage, overriding current file if needed. - * @param int $contextid - * @param string $filearea - * @param int $itemid - * @param string $filepath - * @param string $filename - * @return boolean success - */ - public function copy_to_storage($contextid, $filearea, $itemid, $filepath, $filename) { - return false; - } - - /** - * Copy content of this file to local storage, overriding current file if needed. - * @param string $pathname real local full file name - * @return boolean success - */ - public function copy_to_pathname($pathname) { - return false; - } - - -//TODO: following methods are not implemented yet ;-) - //public abstract function move(location params); - //public abstract function rename(new name); - //public abstract function unzip(location params); - //public abstract function zip(zip file, file info); -} Index: lib/file/file_info_system.php =================================================================== RCS file: lib/file/file_info_system.php diff -N lib/file/file_info_system.php --- lib/file/file_info_system.php 21 May 2009 09:37:21 -0000 1.5 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,119 +0,0 @@ -. - - -/** - * Utility class for browsing of system files. - * - * @package moodlecore - * @subpackage file-browser - * @copyright 2008 Petr Skoda (http://skodak.org) - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -/** - * Represents the system context in the tree navigated by @see{file_browser}. - */ -class file_info_system extends file_info { - public function __construct($browser) { - parent::__construct($browser, get_context_instance(CONTEXT_SYSTEM)); - } - - /** - * Returns list of standard virtual file/directory identification. - * The difference from stored_file parameters is that null values - * are allowed in all fields - * @return array with keys contextid, filearea, itemid, filepath and filename - */ - public function get_params() { - return array('contextid'=>$this->context->id, - 'filearea' =>null, - 'itemid' =>null, - 'filepath' =>null, - 'filename' =>null); - } - - /** - * Returns localised visible name. - * @return string - */ - public function get_visible_name() { - return get_string('arearoot', 'repository'); - } - - /** - * Can I add new files or directories? - * @return bool - */ - public function is_writable() { - return false; - } - - /** - * Is directory? - * @return bool - */ - public function is_directory() { - return true; - } - - /** - * Returns list of children. - * @return array of file_info instances - */ - public function get_children() { - global $DB, $USER; - - $children = array(); - - if ($child = $this->browser->get_file_info(get_context_instance(CONTEXT_USER, $USER->id))) { - $children[] = $child; - } - - $course_cats = $DB->get_records('course_categories', array('parent'=>0), 'sortorder'); - foreach ($course_cats as $category) { - $context = get_context_instance(CONTEXT_COURSECAT, $category->id); - if (!$category->visible and !has_capability('moodle/course:viewhiddencourses', $context)) { - continue; - } - if ($child = $this->browser->get_file_info($context)) { - $children[] = $child; - } - } - - $courses = $DB->get_records('course', array('category'=>0), 'sortorder'); - foreach ($courses as $course) { - if (!$course->visible and !has_capability('moodle/course:viewhiddencourses', $context)) { - continue; - } - $context = get_context_instance(CONTEXT_COURSE, $course->id); - if ($child = $this->browser->get_file_info($context)) { - $children[] = $child; - } - } - - return $children; - } - - /** - * Returns parent file_info instance - * @return file_info or null for root - */ - public function get_parent() { - return null; - } -} Index: lib/file/file_storage.php =================================================================== RCS file: lib/file/file_storage.php diff -N lib/file/file_storage.php --- lib/file/file_storage.php 28 May 2010 07:29:12 -0000 1.42 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,1146 +0,0 @@ -. - - -/** - * Core file storage class definition. - * - * @package moodlecore - * @subpackage file-storage - * @copyright 2008 Petr Skoda {@link http://skodak.org} - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -require_once("$CFG->libdir/file/stored_file.php"); - -/** - * File storage class used for low level access to stored files. - * - * Only owner of file area may use this class to access own files, - * for example only code in mod/assignment/* may access assignment - * attachments. When some other part of moodle needs to access - * files of modules it has to use file_browser class instead or there - * has to be some callback API. - * - * @copyright 2008 Petr Skoda {@link http://skodak.org} - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * @since Moodle 2.0 - */ -class file_storage { - /** @var string Directory with file contents */ - private $filedir; - /** @var string Contents of deleted files not needed any more */ - private $trashdir; - /** @var string tempdir */ - private $tempdir; - /** @var int Permissions for new directories */ - private $dirpermissions; - /** @var int Permissions for new files */ - private $filepermissions; - - /** - * Constructor - do not use directly use @see get_file_storage() call instead. - * - * @param string $filedir full path to pool directory - * @param string $trashdir temporary storage of deleted area - * @param string $tempdir temporary storage of various files - * @param int $dirpermissions new directory permissions - * @param int $filepermissions new file permissions - */ - public function __construct($filedir, $trashdir, $tempdir, $dirpermissions, $filepermissions) { - $this->filedir = $filedir; - $this->trashdir = $trashdir; - $this->tempdir = $tempdir; - $this->dirpermissions = $dirpermissions; - $this->filepermissions = $filepermissions; - - // make sure the file pool directory exists - if (!is_dir($this->filedir)) { - if (!mkdir($this->filedir, $this->dirpermissions, true)) { - throw new file_exception('storedfilecannotcreatefiledirs'); // permission trouble - } - // place warning file in file pool root - if (!file_exists($this->filedir.'/warning.txt')) { - file_put_contents($this->filedir.'/warning.txt', - 'This directory contains the content of uploaded files and is controlled by Moodle code. Do not manually move, change or rename any of the files and subdirectories here.'); - } - } - // make sure the file pool directory exists - if (!is_dir($this->trashdir)) { - if (!mkdir($this->trashdir, $this->dirpermissions, true)) { - throw new file_exception('storedfilecannotcreatefiledirs'); // permission trouble - } - } - } - - /** - * Returns location of filedir (file pool). - * - * Do not use, this method is intended for stored_file instances only!!! - * - * @return string pathname - */ - public function get_filedir() { - return $this->filedir; - } - - /** - * Calculates sha1 hash of unique full path name information. - * - * This hash is a unique file identifier - it is used to improve - * performance and overcome db index size limits. - * - * @param int $contextid - * @param string $filearea - * @param int $itemid - * @param string $filepath - * @param string $filename - * @return string sha1 hash - */ - public static function get_pathname_hash($contextid, $filearea, $itemid, $filepath, $filename) { - return sha1($contextid.$filearea.$itemid.$filepath.$filename); - } - - /** - * Does this file exist? - * - * @param int $contextid - * @param string $filearea - * @param int $itemid - * @param string $filepath - * @param string $filename - * @return bool - */ - public function file_exists($contextid, $filearea, $itemid, $filepath, $filename) { - $filepath = clean_param($filepath, PARAM_PATH); - $filename = clean_param($filename, PARAM_FILE); - - if ($filename === '') { - $filename = '.'; - } - - $pathnamehash = $this->get_pathname_hash($contextid, $filearea, $itemid, $filepath, $filename); - return $this->file_exists_by_hash($pathnamehash); - } - - /** - * Does this file exist? - * - * @param string $pathnamehash - * @return bool - */ - public function file_exists_by_hash($pathnamehash) { - global $DB; - - return $DB->record_exists('files', array('pathnamehash'=>$pathnamehash)); - } - - /** - * Fetch file using local file id. - * - * Please do not rely on file ids, it is usually easier to use - * pathname hashes instead. - * - * @param int $fileid - * @return stored_file instance if exists, false if not - */ - public function get_file_by_id($fileid) { - global $DB; - - if ($file_record = $DB->get_record('files', array('id'=>$fileid))) { - return new stored_file($this, $file_record); - } else { - return false; - } - } - - /** - * Fetch file using local file full pathname hash - * - * @param string $pathnamehash - * @return stored_file instance if exists, false if not - */ - public function get_file_by_hash($pathnamehash) { - global $DB; - - if ($file_record = $DB->get_record('files', array('pathnamehash'=>$pathnamehash))) { - return new stored_file($this, $file_record); - } else { - return false; - } - } - - /** - * Fetch locally stored file. - * - * @param int $contextid - * @param string $filearea - * @param int $itemid - * @param string $filepath - * @param string $filename - * @return stored_file instance if exists, false if not - */ - public function get_file($contextid, $filearea, $itemid, $filepath, $filename) { - global $DB; - - $filepath = clean_param($filepath, PARAM_PATH); - $filename = clean_param($filename, PARAM_FILE); - - if ($filename === '') { - $filename = '.'; - } - - $pathnamehash = $this->get_pathname_hash($contextid, $filearea, $itemid, $filepath, $filename); - return $this->get_file_by_hash($pathnamehash); - } - - /** - * Returns all area files (optionally limited by itemid) - * - * @param int $contextid - * @param string $filearea - * @param int $itemid (all files if not specified) - * @param string $sort - * @param bool $includedirs - * @return array of stored_files indexed by pathanmehash - */ - public function get_area_files($contextid, $filearea, $itemid=false, $sort="sortorder, itemid, filepath, filename", $includedirs = true) { - global $DB; - - $conditions = array('contextid'=>$contextid, 'filearea'=>$filearea); - if ($itemid !== false) { - $conditions['itemid'] = $itemid; - } - - $result = array(); - $file_records = $DB->get_records('files', $conditions, $sort); - foreach ($file_records as $file_record) { - if (!$includedirs and $file_record->filename === '.') { - continue; - } - $result[$file_record->pathnamehash] = new stored_file($this, $file_record); - } - return $result; - } - - /** - * Returns array based tree structure of area files - * - * @param int $contextid - * @param string $filearea - * @param int $itemid - * @return array each dir represented by dirname, subdirs, files and dirfile array elements - */ - public function get_area_tree($contextid, $filearea, $itemid) { - $result = array('dirname'=>'', 'dirfile'=>null, 'subdirs'=>array(), 'files'=>array()); - $files = $this->get_area_files($contextid, $filearea, $itemid, $sort="sortorder, itemid, filepath, filename", true); - // first create directory structure - foreach ($files as $hash=>$dir) { - if (!$dir->is_directory()) { - continue; - } - unset($files[$hash]); - if ($dir->get_filepath() === '/') { - $result['dirfile'] = $dir; - continue; - } - $parts = explode('/', trim($dir->get_filepath(),'/')); - $pointer =& $result; - foreach ($parts as $part) { - if ($part === '') { - continue; - } - if (!isset($pointer['subdirs'][$part])) { - $pointer['subdirs'][$part] = array('dirname'=>$part, 'dirfile'=>null, 'subdirs'=>array(), 'files'=>array()); - } - $pointer =& $pointer['subdirs'][$part]; - } - $pointer['dirfile'] = $dir; - unset($pointer); - } - foreach ($files as $hash=>$file) { - $parts = explode('/', trim($file->get_filepath(),'/')); - $pointer =& $result; - foreach ($parts as $part) { - if ($part === '') { - continue; - } - $pointer =& $pointer['subdirs'][$part]; - } - $pointer['files'][$file->get_filename()] = $file; - unset($pointer); - } - return $result; - } - - /** - * Returns all files and optionally directories - * - * @param int $contextid - * @param string $filearea - * @param int $itemid - * @param int $filepath directory path - * @param bool $recursive include all subdirectories - * @param bool $includedirs include files and directories - * @param string $sort - * @return array of stored_files indexed by pathanmehash - */ - public function get_directory_files($contextid, $filearea, $itemid, $filepath, $recursive = false, $includedirs = true, $sort = "filepath, filename") { - global $DB; - - if (!$directory = $this->get_file($contextid, $filearea, $itemid, $filepath, '.')) { - return array(); - } - - if ($recursive) { - - $dirs = $includedirs ? "" : "AND filename <> '.'"; - $length = textlib_get_instance()->strlen($filepath); - - $sql = "SELECT * - FROM {files} - WHERE contextid = :contextid AND filearea = :filearea AND itemid = :itemid - AND ".$DB->sql_substr("filepath", 1, $length)." = :filepath - AND id <> :dirid - $dirs - ORDER BY $sort"; - $params = array('contextid'=>$contextid, 'filearea'=>$filearea, 'itemid'=>$itemid, 'filepath'=>$filepath, 'dirid'=>$directory->get_id()); - - $files = array(); - $dirs = array(); - $file_records = $DB->get_records_sql($sql, $params); - foreach ($file_records as $file_record) { - if ($file_record->filename == '.') { - $dirs[$file_record->pathnamehash] = new stored_file($this, $file_record); - } else { - $files[$file_record->pathnamehash] = new stored_file($this, $file_record); - } - } - $result = array_merge($dirs, $files); - - } else { - $result = array(); - $params = array('contextid'=>$contextid, 'filearea'=>$filearea, 'itemid'=>$itemid, 'filepath'=>$filepath, 'dirid'=>$directory->get_id()); - - $length = textlib_get_instance()->strlen($filepath); - - if ($includedirs) { - $sql = "SELECT * - FROM {files} - WHERE contextid = :contextid AND filearea = :filearea - AND itemid = :itemid AND filename = '.' - AND ".$DB->sql_substr("filepath", 1, $length)." = :filepath - AND id <> :dirid - ORDER BY $sort"; - $reqlevel = substr_count($filepath, '/') + 1; - $file_records = $DB->get_records_sql($sql, $params); - foreach ($file_records as $file_record) { - if (substr_count($file_record->filepath, '/') !== $reqlevel) { - continue; - } - $result[$file_record->pathnamehash] = new stored_file($this, $file_record); - } - } - - $sql = "SELECT * - FROM {files} - WHERE contextid = :contextid AND filearea = :filearea AND itemid = :itemid - AND filepath = :filepath AND filename <> '.' - ORDER BY $sort"; - - $file_records = $DB->get_records_sql($sql, $params); - foreach ($file_records as $file_record) { - $result[$file_record->pathnamehash] = new stored_file($this, $file_record); - } - } - - return $result; - } - - /** - * Delete all area files (optionally limited by itemid). - * - * @param int $contextid - * @param string $filearea (all areas in context if not specified) - * @param int $itemid (all files if not specified) - * @return bool success - */ - public function delete_area_files($contextid, $filearea = false, $itemid = false) { - global $DB; - - $conditions = array('contextid'=>$contextid); - if ($filearea !== false) { - $conditions['filearea'] = $filearea; - } - if ($itemid !== false) { - $conditions['itemid'] = $itemid; - } - - $file_records = $DB->get_records('files', $conditions); - foreach ($file_records as $file_record) { - $stored_file = new stored_file($this, $file_record); - $stored_file->delete(); - } - - return true; // BC only - } - - /** - * Recursively creates directory. - * - * @param int $contextid - * @param string $filearea - * @param int $itemid - * @param string $filepath - * @param string $filename - * @return bool success - */ - public function create_directory($contextid, $filearea, $itemid, $filepath, $userid = null) { - global $DB; - - // validate all parameters, we do not want any rubbish stored in database, right? - if (!is_number($contextid) or $contextid < 1) { - throw new file_exception('storedfileproblem', 'Invalid contextid'); - } - - $filearea = clean_param($filearea, PARAM_ALPHAEXT); - if ($filearea === '') { - throw new file_exception('storedfileproblem', 'Invalid filearea'); - } - - if (!is_number($itemid) or $itemid < 0) { - throw new file_exception('storedfileproblem', 'Invalid itemid'); - } - - $filepath = clean_param($filepath, PARAM_PATH); - if (strpos($filepath, '/') !== 0 or strrpos($filepath, '/') !== strlen($filepath)-1) { - // path must start and end with '/' - throw new file_exception('storedfileproblem', 'Invalid file path'); - } - - $pathnamehash = $this->get_pathname_hash($contextid, $filearea, $itemid, $filepath, '.'); - - if ($dir_info = $this->get_file_by_hash($pathnamehash)) { - return $dir_info; - } - - static $contenthash = null; - if (!$contenthash) { - $this->add_string_to_pool(''); - $contenthash = sha1(''); - } - - $now = time(); - - $dir_record = new object(); - $dir_record->contextid = $contextid; - $dir_record->filearea = $filearea; - $dir_record->itemid = $itemid; - $dir_record->filepath = $filepath; - $dir_record->filename = '.'; - $dir_record->contenthash = $contenthash; - $dir_record->filesize = 0; - - $dir_record->timecreated = $now; - $dir_record->timemodified = $now; - $dir_record->mimetype = null; - $dir_record->userid = $userid; - - $dir_record->pathnamehash = $pathnamehash; - - $DB->insert_record('files', $dir_record); - $dir_info = $this->get_file_by_hash($pathnamehash); - - if ($filepath !== '/') { - //recurse to parent dirs - $filepath = trim($filepath, '/'); - $filepath = explode('/', $filepath); - array_pop($filepath); - $filepath = implode('/', $filepath); - $filepath = ($filepath === '') ? '/' : "/$filepath/"; - $this->create_directory($contextid, $filearea, $itemid, $filepath, $userid); - } - - return $dir_info; - } - - /** - * Add new local file based on existing local file. - * - * @param mixed $file_record object or array describing changes - * @param mixed $fileorid id or stored_file instance of the existing local file - * @return stored_file instance of newly created file - */ - public function create_file_from_storedfile($file_record, $fileorid) { - global $DB; - - if ($fileorid instanceof stored_file) { - $fid = $fileorid->get_id(); - } else { - $fid = $fileorid; - } - - $file_record = (array)$file_record; // we support arrays too, do not modify the submitted record! - - unset($file_record['id']); - unset($file_record['filesize']); - unset($file_record['contenthash']); - unset($file_record['pathnamehash']); - - $now = time(); - - if (!$newrecord = $DB->get_record('files', array('id'=>$fid))) { - throw new file_exception('storedfileproblem', 'File does not exist'); - } - - unset($newrecord->id); - - foreach ($file_record as $key=>$value) { - // validate all parameters, we do not want any rubbish stored in database, right? - if ($key == 'contextid' and (!is_number($value) or $value < 1)) { - throw new file_exception('storedfileproblem', 'Invalid contextid'); - } - - if ($key == 'filearea') { - $value = clean_param($value, PARAM_ALPHAEXT); - if ($value === '') { - throw new file_exception('storedfileproblem', 'Invalid filearea'); - } - } - - if ($key == 'itemid' and (!is_number($value) or $value < 0)) { - throw new file_exception('storedfileproblem', 'Invalid itemid'); - } - - - if ($key == 'filepath') { - $value = clean_param($value, PARAM_PATH); - if (strpos($value, '/') !== 0 or strrpos($value, '/') !== strlen($value)-1) { - // path must start and end with '/' - throw new file_exception('storedfileproblem', 'Invalid file path'); - } - } - - if ($key == 'filename') { - $value = clean_param($value, PARAM_FILE); - if ($value === '') { - // path must start and end with '/' - throw new file_exception('storedfileproblem', 'Invalid file name'); - } - } - - $newrecord->$key = $value; - } - - $newrecord->pathnamehash = $this->get_pathname_hash($newrecord->contextid, $newrecord->filearea, $newrecord->itemid, $newrecord->filepath, $newrecord->filename); - - if ($newrecord->filename === '.') { - // special case - only this function supports directories ;-) - $directory = $this->create_directory($newrecord->contextid, $newrecord->filearea, $newrecord->itemid, $newrecord->filepath, $newrecord->userid); - // update the existing directory with the new data - $newrecord->id = $directory->get_id(); - $DB->update_record('files', $newrecord); - return new stored_file($this, $newrecord); - } - - try { - $newrecord->id = $DB->insert_record('files', $newrecord); - } catch (database_exception $e) { - $newrecord->id = false; - } - - if (!$newrecord->id) { - throw new stored_file_creation_exception($newrecord->contextid, $newrecord->filearea, $newrecord->itemid, - $newrecord->filepath, $newrecord->filename); - } - - $this->create_directory($newrecord->contextid, $newrecord->filearea, $newrecord->itemid, $newrecord->filepath, $newrecord->userid); - - return new stored_file($this, $newrecord); - } - - /** - * Add new local file. - * - * @param mixed $file_record object or array describing file - * @param string $path path to file or content of file - * @param array $options @see download_file_content() options - * @param bool $usetempfile use temporary file for download, may prevent out of memory problems - * @return stored_file instance - */ - public function create_file_from_url($file_record, $url, array $options = NULL, $usetempfile = false) { - - $file_record = (array)$file_record; //do not modify the submitted record, this cast unlinks objects - $file_record = (object)$file_record; // we support arrays too - - $headers = isset($options['headers']) ? $options['headers'] : null; - $postdata = isset($options['postdata']) ? $options['postdata'] : null; - $fullresponse = isset($options['fullresponse']) ? $options['fullresponse'] : false; - $timeout = isset($options['timeout']) ? $options['timeout'] : 300; - $connecttimeout = isset($options['connecttimeout']) ? $options['connecttimeout'] : 20; - $skipcertverify = isset($options['skipcertverify']) ? $options['skipcertverify'] : false; - - if (!isset($file_record->filename)) { - $parts = explode('/', $url); - $filename = array_pop($parts); - $file_record->filename = clean_param($filename, PARAM_FILE); - } - $source = !empty($file_record->source) ? $file_record->source : $url; - $file_record->source = clean_param($source, PARAM_URL); - - if ($usetempfile) { - check_dir_exists($this->tempdir, true, true); - $tmpfile = tempnam($this->tempdir, 'newfromurl'); - $content = download_file_content($url, $headers, $postdata, $fullresponse, $timeout, $connecttimeout, $skipcertverify, $tmpfile); - if ($content === false) { - throw new file_exception('storedfileproblem', 'Can not fetch file form URL'); - } - try { - $newfile = $this->create_file_from_pathname($file_record, $tmpfile); - @unlink($tmpfile); - return $newfile; - } catch (Exception $e) { - @unlink($tmpfile); - throw $e; - } - - } else { - $content = download_file_content($url, $headers, $postdata, $fullresponse, $timeout, $connecttimeout, $skipcertverify); - if ($content === false) { - throw new file_exception('storedfileproblem', 'Can not fetch file form URL'); - } - return $this->create_file_from_string($file_record, $content); - } - } - - /** - * Add new local file. - * - * @param mixed $file_record object or array describing file - * @param string $path path to file or content of file - * @return stored_file instance - */ - public function create_file_from_pathname($file_record, $pathname) { - global $DB; - - $file_record = (array)$file_record; //do not modify the submitted record, this cast unlinks objects - $file_record = (object)$file_record; // we support arrays too - - // validate all parameters, we do not want any rubbish stored in database, right? - if (!is_number($file_record->contextid) or $file_record->contextid < 1) { - throw new file_exception('storedfileproblem', 'Invalid contextid'); - } - - $file_record->filearea = clean_param($file_record->filearea, PARAM_ALPHAEXT); - if ($file_record->filearea === '') { - throw new file_exception('storedfileproblem', 'Invalid filearea'); - } - - if (!is_number($file_record->itemid) or $file_record->itemid < 0) { - throw new file_exception('storedfileproblem', 'Invalid itemid'); - } - - if (!empty($file_record->sortorder)) { - if (!is_number($file_record->sortorder) or $file_record->sortorder < 0) { - $file_record->sortorder = 0; - } - } else { - $file_record->sortorder = 0; - } - - $file_record->filepath = clean_param($file_record->filepath, PARAM_PATH); - if (strpos($file_record->filepath, '/') !== 0 or strrpos($file_record->filepath, '/') !== strlen($file_record->filepath)-1) { - // path must start and end with '/' - throw new file_exception('storedfileproblem', 'Invalid file path'); - } - - $file_record->filename = clean_param($file_record->filename, PARAM_FILE); - if ($file_record->filename === '') { - // filename must not be empty - throw new file_exception('storedfileproblem', 'Invalid file name'); - } - - $now = time(); - - $newrecord = new object(); - - $newrecord->contextid = $file_record->contextid; - $newrecord->filearea = $file_record->filearea; - $newrecord->itemid = $file_record->itemid; - $newrecord->filepath = $file_record->filepath; - $newrecord->filename = $file_record->filename; - - $newrecord->timecreated = empty($file_record->timecreated) ? $now : $file_record->timecreated; - $newrecord->timemodified = empty($file_record->timemodified) ? $now : $file_record->timemodified; - $newrecord->mimetype = empty($file_record->mimetype) ? mimeinfo('type', $file_record->filename) : $file_record->mimetype; - $newrecord->userid = empty($file_record->userid) ? null : $file_record->userid; - $newrecord->source = empty($file_record->source) ? null : $file_record->source; - $newrecord->author = empty($file_record->author) ? null : $file_record->author; - $newrecord->license = empty($file_record->license) ? null : $file_record->license; - $newrecord->sortorder = $file_record->sortorder; - - list($newrecord->contenthash, $newrecord->filesize, $newfile) = $this->add_file_to_pool($pathname); - - $newrecord->pathnamehash = $this->get_pathname_hash($newrecord->contextid, $newrecord->filearea, $newrecord->itemid, $newrecord->filepath, $newrecord->filename); - - try { - $newrecord->id = $DB->insert_record('files', $newrecord); - } catch (database_exception $e) { - $newrecord->id = false; - } - - if (!$newrecord->id) { - if ($newfile) { - $this->deleted_file_cleanup($newrecord->contenthash); - } - throw new stored_file_creation_exception($newrecord->contextid, $newrecord->filearea, $newrecord->itemid, - $newrecord->filepath, $newrecord->filename); - } - - $this->create_directory($newrecord->contextid, $newrecord->filearea, $newrecord->itemid, $newrecord->filepath, $newrecord->userid); - - return new stored_file($this, $newrecord); - } - - /** - * Add new local file. - * - * @param mixed $file_record object or array describing file - * @param string $content content of file - * @return stored_file instance - */ - public function create_file_from_string($file_record, $content) { - global $DB; - - $file_record = (array)$file_record; //do not modify the submitted record, this cast unlinks objects - $file_record = (object)$file_record; // we support arrays too - - // validate all parameters, we do not want any rubbish stored in database, right? - if (!is_number($file_record->contextid) or $file_record->contextid < 1) { - throw new file_exception('storedfileproblem', 'Invalid contextid'); - } - - $file_record->filearea = clean_param($file_record->filearea, PARAM_ALPHAEXT); - if ($file_record->filearea === '') { - throw new file_exception('storedfileproblem', 'Invalid filearea'); - } - - if (!is_number($file_record->itemid) or $file_record->itemid < 0) { - throw new file_exception('storedfileproblem', 'Invalid itemid'); - } - - if (!empty($file_record->sortorder)) { - if (!is_number($file_record->sortorder) or $file_record->sortorder < 0) { - $file_record->sortorder = 0; - } - } else { - $file_record->sortorder = 0; - } - - $file_record->filepath = clean_param($file_record->filepath, PARAM_PATH); - if (strpos($file_record->filepath, '/') !== 0 or strrpos($file_record->filepath, '/') !== strlen($file_record->filepath)-1) { - // path must start and end with '/' - throw new file_exception('storedfileproblem', 'Invalid file path'); - } - - $file_record->filename = clean_param($file_record->filename, PARAM_FILE); - if ($file_record->filename === '') { - // path must start and end with '/' - throw new file_exception('storedfileproblem', 'Invalid file name'); - } - - $now = time(); - - $newrecord = new object(); - - $newrecord->contextid = $file_record->contextid; - $newrecord->filearea = $file_record->filearea; - $newrecord->itemid = $file_record->itemid; - $newrecord->filepath = $file_record->filepath; - $newrecord->filename = $file_record->filename; - - $newrecord->timecreated = empty($file_record->timecreated) ? $now : $file_record->timecreated; - $newrecord->timemodified = empty($file_record->timemodified) ? $now : $file_record->timemodified; - $newrecord->mimetype = empty($file_record->mimetype) ? mimeinfo('type', $file_record->filename) : $file_record->mimetype; - $newrecord->userid = empty($file_record->userid) ? null : $file_record->userid; - $newrecord->source = empty($file_record->source) ? null : $file_record->source; - $newrecord->author = empty($file_record->author) ? null : $file_record->author; - $newrecord->license = empty($file_record->license) ? null : $file_record->license; - $newrecord->sortorder = $file_record->sortorder; - - list($newrecord->contenthash, $newrecord->filesize, $newfile) = $this->add_string_to_pool($content); - - $newrecord->pathnamehash = $this->get_pathname_hash($newrecord->contextid, $newrecord->filearea, $newrecord->itemid, $newrecord->filepath, $newrecord->filename); - - try { - $newrecord->id = $DB->insert_record('files', $newrecord); - } catch (database_exception $e) { - $newrecord->id = false; - } - - if (!$newrecord->id) { - if ($newfile) { - $this->deleted_file_cleanup($newrecord->contenthash); - } - throw new stored_file_creation_exception($newrecord->contextid, $newrecord->filearea, $newrecord->itemid, - $newrecord->filepath, $newrecord->filename); - } - - $this->create_directory($newrecord->contextid, $newrecord->filearea, $newrecord->itemid, $newrecord->filepath, $newrecord->userid); - - return new stored_file($this, $newrecord); - } - - /** - * Creates new image file from existing. - * - * @param mixed $file_record object or array describing new file - * @param mixed file id or stored file object - * @param int $newwidth in pixels - * @param int $newheight in pixels - * @param bool $keepaspectratio - * @param int $quality depending on image type 0-100 for jpeg, 0-9 (0 means no compression) for png - * @return stored_file instance - */ - public function convert_image($file_record, $fid, $newwidth = NULL, $newheight = NULL, $keepaspectratio = true, $quality = NULL) { - global $DB; - - if ($fid instanceof stored_file) { - $fid = $fid->get_id(); - } - - $file_record = (array)$file_record; // we support arrays too, do not modify the submitted record! - - if (!$file = $this->get_file_by_id($fid)) { // make sure file really exists and we we correct data - throw new file_exception('storedfileproblem', 'File does not exist'); - } - - if (!$imageinfo = $file->get_imageinfo()) { - throw new file_exception('storedfileproblem', 'File is not an image'); - } - - if (!isset($file_record['filename'])) { - $file_record['filename'] == $file->get_filename(); - } - - if (!isset($file_record['mimetype'])) { - $file_record['mimetype'] = mimeinfo('type', $file_record['filename']); - } - - $width = $imageinfo['width']; - $height = $imageinfo['height']; - $mimetype = $imageinfo['mimetype']; - - if ($keepaspectratio) { - if (0 >= $newwidth and 0 >= $newheight) { - // no sizes specified - $newwidth = $width; - $newheight = $height; - - } else if (0 < $newwidth and 0 < $newheight) { - $xheight = ($newwidth*($height/$width)); - if ($xheight < $newheight) { - $newheight = (int)$xheight; - } else { - $newwidth = (int)($newheight*($width/$height)); - } - - } else if (0 < $newwidth) { - $newheight = (int)($newwidth*($height/$width)); - - } else { //0 < $newheight - $newwidth = (int)($newheight*($width/$height)); - } - - } else { - if (0 >= $newwidth) { - $newwidth = $width; - } - if (0 >= $newheight) { - $newheight = $height; - } - } - - $img = imagecreatefromstring($file->get_content()); - if ($height != $newheight or $width != $newwidth) { - $newimg = imagecreatetruecolor($newwidth, $newheight); - if (!imagecopyresized($newimg, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height)) { - // weird - throw new file_exception('storedfileproblem', 'Can not resize image'); - } - imagedestroy($img); - $img = $newimg; - } - - ob_start(); - switch ($file_record['mimetype']) { - case 'image/gif': - imagegif($img); - break; - - case 'image/jpeg': - if (is_null($quality)) { - imagejpeg($img); - } else { - imagejpeg($img, NULL, $quality); - } - break; - - case 'image/png': - $quality = (int)$quality; - imagepng($img, NULL, $quality, NULL); - break; - - default: - throw new file_exception('storedfileproblem', 'Unsupported mime type'); - } - - $content = ob_get_contents(); - ob_end_clean(); - imagedestroy($img); - - if (!$content) { - throw new file_exception('storedfileproblem', 'Can not convert image'); - } - - return $this->create_file_from_string($file_record, $content); - } - - /** - * Add file content to sha1 pool. - * - * @param string $pathname path to file - * @param string $contenthash sha1 hash of content if known (performance only) - * @return array (contenthash, filesize, newfile) - */ - public function add_file_to_pool($pathname, $contenthash = NULL) { - if (!is_readable($pathname)) { - throw new file_exception('storedfilecannotread'); - } - - if (is_null($contenthash)) { - $contenthash = sha1_file($pathname); - } - - $filesize = filesize($pathname); - - $hashpath = $this->path_from_hash($contenthash); - $hashfile = "$hashpath/$contenthash"; - - if (file_exists($hashfile)) { - if (filesize($hashfile) !== $filesize) { - throw new file_pool_content_exception($contenthash); - } - $newfile = false; - - } else { - if (!is_dir($hashpath)) { - if (!mkdir($hashpath, $this->dirpermissions, true)) { - throw new file_exception('storedfilecannotcreatefiledirs'); // permission trouble - } - } - $newfile = true; - - if (!copy($pathname, $hashfile)) { - throw new file_exception('storedfilecannotread'); - } - - if (filesize($hashfile) !== $filesize) { - @unlink($hashfile); - throw new file_pool_content_exception($contenthash); - } - chmod($hashfile, $this->filepermissions); // fix permissions if needed - } - - - return array($contenthash, $filesize, $newfile); - } - - /** - * Add string content to sha1 pool. - * - * @param string $content file content - binary string - * @return array (contenthash, filesize, newfile) - */ - public function add_string_to_pool($content) { - $contenthash = sha1($content); - $filesize = strlen($content); // binary length - - $hashpath = $this->path_from_hash($contenthash); - $hashfile = "$hashpath/$contenthash"; - - - if (file_exists($hashfile)) { - if (filesize($hashfile) !== $filesize) { - throw new file_pool_content_exception($contenthash); - } - $newfile = false; - - } else { - if (!is_dir($hashpath)) { - if (!mkdir($hashpath, $this->dirpermissions, true)) { - throw new file_exception('storedfilecannotcreatefiledirs'); // permission trouble - } - } - $newfile = true; - - file_put_contents($hashfile, $content); - - if (filesize($hashfile) !== $filesize) { - @unlink($hashfile); - throw new file_pool_content_exception($contenthash); - } - chmod($hashfile, $this->filepermissions); // fix permissions if needed - } - - return array($contenthash, $filesize, $newfile); - } - - /** - * Return path to file with given hash. - * - * NOTE: must not be public, files in pool must not be modified - * - * @param string $contenthash - * @return string expected file location - */ - protected function path_from_hash($contenthash) { - $l1 = $contenthash[0].$contenthash[1]; - $l2 = $contenthash[2].$contenthash[3]; - $l3 = $contenthash[4].$contenthash[5]; - return "$this->filedir/$l1/$l2/$l3"; - } - - /** - * Return path to file with given hash. - * - * NOTE: must not be public, files in pool must not be modified - * - * @param string $contenthash - * @return string expected file location - */ - protected function trash_path_from_hash($contenthash) { - $l1 = $contenthash[0].$contenthash[1]; - $l2 = $contenthash[2].$contenthash[3]; - $l3 = $contenthash[4].$contenthash[5]; - return "$this->trashdir/$l1/$l2/$l3"; - } - - /** - * Tries to recover missing content of file from trash. - * - * @param object $file_record - * @return bool success - */ - public function try_content_recovery($file) { - $contenthash = $file->get_contenthash(); - $trashfile = $this->trash_path_from_hash($contenthash).'/'.$contenthash; - if (!is_readable($trashfile)) { - if (!is_readable($this->trashdir.'/'.$contenthash)) { - return false; - } - // nice, at least alternative trash file in trash root exists - $trashfile = $this->trashdir.'/'.$contenthash; - } - if (filesize($trashfile) != $file->get_filesize() or sha1_file($trashfile) != $contenthash) { - //weird, better fail early - return false; - } - $contentdir = $this->path_from_hash($contenthash); - $contentfile = $contentdir.'/'.$contenthash; - if (file_exists($contentfile)) { - //strange, no need to recover anything - return true; - } - if (!is_dir($contentdir)) { - if (!mkdir($contentdir, $this->dirpermissions, true)) { - return false; - } - } - return rename($trashfile, $contentfile); - } - - /** - * Marks pool file as candidate for deleting. - * - * DO NOT call directly - reserved for core!! - * - * @param string $contenthash - * @return void - */ - public function deleted_file_cleanup($contenthash) { - global $DB; - - //Note: this section is critical - in theory file could be reused at the same - // time, if this happens we can still recover the file from trash - if ($DB->record_exists('files', array('contenthash'=>$contenthash))) { - // file content is still used - return; - } - //move content file to trash - $contentfile = $this->path_from_hash($contenthash).'/'.$contenthash; - if (!file_exists($contentfile)) { - //weird, but no problem - return; - } - $trashpath = $this->trash_path_from_hash($contenthash); - $trashfile = $trashpath.'/'.$contenthash; - if (file_exists($trashfile)) { - // we already have this content in trash, no need to move it there - unlink($contentfile); - return; - } - if (!is_dir($trashpath)) { - mkdir($trashpath, $this->dirpermissions, true); - } - rename($contentfile, $trashfile); - chmod($trashfile, $this->filepermissions); // fix permissions if needed - } - - /** - * Cron cleanup job. - * - * @return void - */ - public function cron() { - global $CFG, $DB; - // remove trash pool files once a day - // if you want to disable purging of trash put $CFG->fileslastcleanup=time(); into config.php - if (empty($CFG->fileslastcleanup) or $CFG->fileslastcleanup < time() - 60*60*24) { - require_once($CFG->libdir.'/filelib.php'); - // Delete files that are associated with a context that no longer exists. - mtrace('Cleaning up files from deleted contexts... ', ''); - $sql = "SELECT DISTINCT f.contextid - FROM {files} f - LEFT OUTER JOIN {context} c ON f.contextid = c.id - WHERE c.id IS NULL"; - if ($rs = $DB->get_recordset_sql($sql)) { - $fs = get_file_storage(); - foreach ($rs as $ctx) { - $fs->delete_area_files($ctx->contextid); - } - } - mtrace('done.'); - - mtrace('Deleting trash files... ', ''); - fulldelete($this->trashdir); - set_config('fileslastcleanup', time()); - mtrace('done.'); - } - } -} - Index: lib/file/file_browser.php =================================================================== RCS file: lib/file/file_browser.php diff -N lib/file/file_browser.php --- lib/file/file_browser.php 21 Jun 2010 15:31:02 -0000 1.32 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,575 +0,0 @@ -. - - -/** - * Utility class for browsing of files. - * - * @package moodlecore - * @subpackage file-browser - * @copyright 2008 Petr Skoda (http://skodak.org) - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -require_once("$CFG->libdir/file/file_info.php"); -require_once("$CFG->libdir/file/file_info_module.php"); -require_once("$CFG->libdir/file/file_info_stored.php"); -require_once("$CFG->libdir/file/file_info_system.php"); -require_once("$CFG->libdir/file/file_info_user.php"); -require_once("$CFG->libdir/file/file_info_coursecat.php"); -require_once("$CFG->libdir/file/file_info_course.php"); -require_once("$CFG->libdir/file/file_info_coursesection.php"); -require_once("$CFG->libdir/file/file_info_coursesectionbackup.php"); -require_once("$CFG->libdir/file/file_info_coursefile.php"); -require_once("$CFG->libdir/file/virtual_root_file.php"); - -/** - * This class provides the main entry point for other code wishing to get - * information about files. - * - * The whole file storage for a Moodle site can be seen as a huge virtual tree. - * The spine of the tree is the tree of contexts (system, course-categories, - * courses, modules, also users). Then, within each context, there may be any number of - * file areas, and a file area contains folders and files. The various file_info - * subclasses return info about the things in this tree. They should be obtained - * from an instance of this class. - * - * This virtual tree is different for each user depending of his/her current permissions. - * Some branches such as draft areas are hidden, but accessible. - * - * Always use this abstraction when you need to access module files from core code. - */ -class file_browser { - - /** - * Looks up file_info instance - * @param object $context - * @param string $filearea - * @param int $itemid - * @param string $filepath - * @param string $filename - * @return file_info instance or null if not found or access not allowed - */ - public function get_file_info($context, $filearea=null, $itemid=null, $filepath=null, $filename=null) { - switch ($context->contextlevel) { - case CONTEXT_SYSTEM: - return $this->get_file_info_system($context, $filearea, $itemid, $filepath, $filename); - case CONTEXT_USER: - return $this->get_file_info_user($context, $filearea, $itemid, $filepath, $filename); - case CONTEXT_COURSECAT: - return $this->get_file_info_coursecat($context, $filearea, $itemid, $filepath, $filename); - case CONTEXT_COURSE: - return $this->get_file_info_course($context, $filearea, $itemid, $filepath, $filename); - case CONTEXT_MODULE: - return $this->get_file_info_module($context, $filearea, $itemid, $filepath, $filename); - } - - return null; - } - - /** - * Returns info about the files at System context - * @param object $context - * @param string $filearea - * @return file_info_system - */ - private function get_file_info_system($context, $filearea=null) { - if (is_null($filearea)) { - return new file_info_system($this); - } - //TODO: question files browsing - - return null; - } - - /** - * Returns info about the files at User context - * @param object $context - * @param string $filearea - * @return file_info_system - */ - private function get_file_info_user($context, $filearea=null, $itemid=null, $filepath=null, $filename=null) { - global $USER, $DB; - if ($context->instanceid == $USER->id) { - $user = $USER; - } else { - $user = $DB->get_record('user', array('id'=>$context->instanceid)); - } - - if (isguestuser($user) or empty($user->id)) { - // no guests or not logged in users here - return null; - } - - if ($user->deleted) { - return null; - } - - if (is_null($filearea)) { - // access control: list areas only for myself - if ($context->instanceid != $USER->id) { - return null; - } - - return new file_info_user($this, $context); - - } else { - $methodname = "get_file_info_$filearea"; - if (method_exists($this, $methodname)) { - return $this->$methodname($user, $context, $filearea, $itemid, $filepath, $filename); - } - } - - return null; - } - - private function get_file_info_user_private($user, $context, $filearea=null, $itemid=null, $filepath=null, $filename=null) { - global $USER, $CFG; - - $fs = get_file_storage(); - - // access control: only my files for now, nobody else - if ($context->instanceid != $USER->id) { - return null; - } - - if (is_null($itemid)) { - return new file_info_user($this, $context); - } - $filepath = is_null($filepath) ? '/' : $filepath; - $filename = is_null($filename) ? '.' : $filename; - - if (!$storedfile = $fs->get_file($context->id, $filearea, 0, $filepath, $filename)) { - if ($filepath === '/' and $filename === '.') { - $storedfile = new virtual_root_file($context->id, $filearea, 0); - } else { - // not found - return null; - } - } - $urlbase = $CFG->wwwroot.'/userfile.php'; - return new file_info_stored($this, $context, $storedfile, $urlbase, get_string('areauserpersonal', 'repository'), false, true, true, false); - } - - private function get_file_info_user_profile($user, $context, $filearea=null, $itemid=null, $filepath=null, $filename=null) { - global $USER, $CFG; - - $fs = get_file_storage(); - - if (is_null($itemid)) { - return new file_info_user($this, $context); - } - - // access controll here must match user edit forms - if ($user->id == $USER->id) { - if (!has_capability('moodle/user:editownprofile', get_context_instance(CONTEXT_SYSTEM))) { - return null; - } - } else { - if (!has_capability('moodle/user:editprofile', $context) and !has_capability('moodle/user:update', $context)) { - return null; - } - } - - $filepath = is_null($filepath) ? '/' : $filepath; - $filename = is_null($filename) ? '.' : $filename; - - if (!$storedfile = $fs->get_file($context->id, $filearea, 0, $filepath, $filename)) { - if ($filepath === '/' and $filename === '.') { - $storedfile = new virtual_root_file($context->id, $filearea, 0); - } else { - // not found - return null; - } - } - $urlbase = $CFG->wwwroot.'/userfile.php'; - return new file_info_stored($this, $context, $storedfile, $urlbase, get_string('areauserprofile', 'repository'), false, true, true, false); - } - - private function get_file_info_user_draft($user, $context, $filearea=null, $itemid=null, $filepath=null, $filename=null) { - global $USER, $CFG; - - $fs = get_file_storage(); - - // access control: only my files - if ($context->instanceid != $USER->id) { - return null; - } - - if (empty($itemid)) { - // do not browse itemids - you most know the draftid to see what is there - return null; - } - $urlbase = $CFG->wwwroot.'/draftfile.php'; - - $filepath = is_null($filepath) ? '/' : $filepath; - $filename = is_null($filename) ? '.' : $filename; - - if (!$storedfile = $fs->get_file($context->id, $filearea, $itemid, $filepath, $filename)) { - if ($filepath === '/' and $filename === '.') { - $storedfile = new virtual_root_file($context->id, $filearea, $itemid); - } else { - // not found - return null; - } - } - return new file_info_stored($this, $context, $storedfile, $urlbase, get_string('areauserdraft', 'repository'), true, true, true, true); - } - - private function get_file_info_user_backup($user, $context, $filearea=null, $itemid=null, $filepath=null, $filename=null) { - global $USER, $CFG; - - $fs = get_file_storage(); - - // only current user can access this area - if ($context->instanceid != $USER->id) { - return null; - } - if ($USER->id != $user->id) { - return null; - } - - $urlbase = $CFG->wwwroot.'/userfile.php'; - - $filepath = is_null($filepath) ? '/' : $filepath; - $filename = is_null($filename) ? '.' : $filename; - - if (!$storedfile = $fs->get_file($context->id, $filearea, $itemid, $filepath, $filename)) { - if ($filepath === '/' and $filename === '.') { - $storedfile = new virtual_root_file($context->id, $filearea, 0); - } else { - // not found - return null; - } - } - return new file_info_stored($this, $context, $storedfile, $urlbase, get_string('areauserbackup', 'repository'), false, true, true, false); - } - - private function get_file_info_coursecat($context, $filearea=null, $itemid=null, $filepath=null, $filename=null) { - global $DB, $CFG; - - $fs = get_file_storage(); - - if (!$category = $DB->get_record('course_categories', array('id'=>$context->instanceid))) { - return null; - } - - if (!$category->visible and !has_capability('moodle/course:viewhiddencourses', $context)) { - return null; - } - - if (!is_null($filearea) and !in_array($filearea, array('coursecat_intro'))) { - // file area does not exist, sorry - $filearea = null; - } - - if (is_null($filearea) or is_null($itemid)) { - return new file_info_coursecat($this, $context, $category); - - } else { - if ($filearea == 'coursecat_intro') { - if (!has_capability('moodle/course:update', $context)) { - return null; - } - - $filepath = is_null($filepath) ? '/' : $filepath; - $filename = is_null($filename) ? '.' : $filename; - - $urlbase = $CFG->wwwroot.'/pluginfile.php'; - if (!$storedfile = $fs->get_file($context->id, $filearea, 0, $filepath, $filename)) { - if ($filepath === '/' and $filename === '.') { - $storedfile = new virtual_root_file($context->id, $filearea, 0); - } else { - // not found - return null; - } - } - return new file_info_stored($this, $context, $storedfile, $urlbase, get_string('areacategoryintro', 'repository'), false, true, true, false); - } - } - - return null; - } - - private function get_file_info_course($context, $filearea=null, $itemid=null, $filepath=null, $filename=null) { - global $DB, $COURSE; - - if ($context->instanceid == $COURSE->id) { - $course = $COURSE; - } else if (!$course = $DB->get_record('course', array('id'=>$context->instanceid))) { - return null; - } - - if (!$course->visible and !has_capability('moodle/course:viewhiddencourses', $context)) { - return null; - } - - if (!is_null($filearea) and !in_array($filearea, array('course_summary', 'course_content', 'course_section', 'course_backup', 'section_backup'))) { - // file area does not exist, sorry - $filearea = null; - } - - if ($filearea === 'course_content' and $course->legacyfiles != 2) { - // bad luck, legacy course files not used any more - return null; - } - - $filepath = is_null($filepath) ? '/' : $filepath; - $filename = is_null($filename) ? '.' : $filename; - - if (is_null($filearea)) { - return new file_info_course($this, $context, $course); - - } else { - $methodname = "get_file_info_$filearea"; - if (method_exists($this, $methodname)) { - return $this->$methodname($course, $context, $filearea, $itemid, $filepath, $filename); - } - } - - return null; - } - - private function get_file_info_course_summary($course, $context, $filearea=null, $itemid=null, $filepath=null, $filename=null) { - global $CFG; - - $fs = get_file_storage(); - - if (!has_capability('moodle/course:update', $context)) { - return null; - } - if (is_null($itemid)) { - return new file_info_course($this, $context, $course); - } - - $urlbase = $CFG->wwwroot.'/pluginfile.php'; - if (!$storedfile = $fs->get_file($context->id, $filearea, 0, $filepath, $filename)) { - if ($filepath === '/' and $filename === '.') { - $storedfile = new virtual_root_file($context->id, $filearea, 0); - } else { - // not found - return null; - } - } - return new file_info_stored($this, $context, $storedfile, $urlbase, get_string('areacourseintro', 'repository'), false, true, true, false); - - } - - private function get_file_info_course_section($course, $context, $filearea=null, $itemid=null, $filepath=null, $filename=null) { - global $CFG, $DB; - - $fs = get_file_storage(); - - if (!has_capability('moodle/course:update', $context)) { - return null; - } - $urlbase = $CFG->wwwroot.'/pluginfile.php'; - - if (empty($itemid)) { - // list all sections - return new file_info_coursesection($this, $context, $course); - } - - if (!$section = $DB->get_record('course_sections', array('course'=>$course->id, 'id'=>$itemid))) { - return null; // does not exist - } - - if (!$storedfile = $fs->get_file($context->id, $filearea, $itemid, $filepath, $filename)) { - if ($filepath === '/' and $filename === '.') { - $storedfile = new virtual_root_file($context->id, $filearea, $itemid); - } else { - // not found - return null; - } - } - return new file_info_stored($this, $context, $storedfile, $urlbase, $section->section, true, true, true, false); - - } - - private function get_file_info_course_backup($course, $context, $filearea=null, $itemid=null, $filepath=null, $filename=null) { - global $CFG; - - $fs = get_file_storage(); - - if (!has_capability('moodle/backup:backupcourse', $context) and !has_capability('moodle/restore:restorecourse', $context)) { - return null; - } - if (is_null($itemid)) { - return new file_info_course($this, $context, $course); - } - - $urlbase = $CFG->wwwroot.'/pluginfile.php'; - if (!$storedfile = $fs->get_file($context->id, $filearea, 0, $filepath, $filename)) { - if ($filepath === '/' and $filename === '.') { - $storedfile = new virtual_root_file($context->id, $filearea, 0); - } else { - // not found - return null; - } - } - - $downloadable = has_capability('moodle/backup:downloadfile', $context); - $uploadable = has_capability('moodle/restore:uploadfile', $context); - return new file_info_stored($this, $context, $storedfile, $urlbase, get_string('coursebackup', 'repository'), false, $downloadable, $uploadable, false); - - } - - private function get_file_info_section_backup($course, $context, $filearea=null, $itemid=null, $filepath=null, $filename=null) { - global $CFG, $DB; - - if (!has_capability('moodle/backup:backupcourse', $context) and !has_capability('moodle/restore:restorecourse', $context)) { - return null; - } - - $fs = get_file_storage(); - if (empty($itemid)) { - // list all sections - return new file_info_coursesectionbackup($this, $context, $course); - } - - if (!$section = $DB->get_record('course_sections', array('course'=>$course->id, 'id'=>$itemid))) { - return null; // does not exist - } - - - $urlbase = $CFG->wwwroot.'/pluginfile.php'; - if (!$storedfile = $fs->get_file($context->id, $filearea, $itemid, $filepath, $filename)) { - if ($filepath === '/' and $filename === '.') { - $storedfile = new virtual_root_file($context->id, $filearea, $itemid); - } else { - // not found - return null; - } - } - - $downloadable = has_capability('moodle/backup:downloadfile', $context); - $uploadable = has_capability('moodle/restore:uploadfile', $context); - return new file_info_stored($this, $context, $storedfile, $urlbase, $section->id, true, $downloadable, $uploadable, false); - } - - private function get_file_info_course_content($course, $context, $filearea=null, $itemid=null, $filepath=null, $filename=null) { - $fs = get_file_storage(); - - if (!has_capability('moodle/course:managefiles', $context)) { - return null; - } - if (is_null($itemid)) { - return new file_info_course($this, $context, $course); - } - - if (!$storedfile = $fs->get_file($context->id, $filearea, 0, $filepath, $filename)) { - if ($filepath === '/' and $filename === '.') { - $storedfile = new virtual_root_file($context->id, $filearea, 0); - } else { - // not found - return null; - } - } - - return new file_info_coursefile($this, $context, $storedfile); - } - - private function get_file_info_module($context, $filearea=null, $itemid=null, $filepath=null, $filename=null) { - global $COURSE, $DB, $CFG; - - $fs = get_file_storage(); - - if (!$cm = get_coursemodule_from_id('', $context->instanceid)) { - return null; - } - - if ($cm->course == $COURSE->id) { - $course = $COURSE; - } else if (!$course = $DB->get_record('course', array('id'=>$cm->course))) { - return null; - } - - $modinfo = get_fast_modinfo($course); - - if (empty($modinfo->cms[$cm->id]->uservisible)) { - return null; - } - - $modname = $modinfo->cms[$cm->id]->modname; - - $libfile = "$CFG->dirroot/mod/$modname/lib.php"; - if (!file_exists($libfile)) { - return null; - } - require_once($libfile); - - $fileinfofunction = $modname.'_get_file_areas'; - if (function_exists($fileinfofunction)) { - $areas = $fileinfofunction($course, $cm, $context); - } else { - $areas = array(); - } - if (!isset($areas[$modname.'_intro']) - and plugin_supports('mod', $modname, FEATURE_MOD_INTRO, true) - and has_capability('moodle/course:managefiles', $context)) { - $areas = array_merge(array($modname.'_intro'=>get_string('moduleintro')), $areas); - } - - if (has_capability('moodle/backup:downloadfile', $context)) { - $areas = array_merge(array('activity_backup'=>get_string('activitybackup', 'repository')), $areas); - } - - if (empty($areas)) { - return null; - } - - if ($filearea === $modname.'_intro' || $filearea === 'activity_backup') { - // always only itemid 0 - if (!has_capability('moodle/course:managefiles', $context)) { - return null; - } - // need downloadfile cap when accessing activity_backup area - if ($filearea === 'activity_backup' && !has_capability('moodle/backup:downloadfile', $context)) { - return null; - } - - $filepath = is_null($filepath) ? '/' : $filepath; - $filename = is_null($filename) ? '.' : $filename; - - $urlbase = $CFG->wwwroot.'/pluginfile.php'; - if (!$storedfile = $fs->get_file($context->id, $filearea, 0, $filepath, $filename)) { - if ($filepath === '/' and $filename === '.') { - $storedfile = new virtual_root_file($context->id, $filearea, 0); - } else { - // not found - return null; - } - } - return new file_info_stored($this, $context, $storedfile, $urlbase, $areas[$filearea], false, true, true, false); - - } else if (is_null($filearea)) { - // modules have to decide if they want to use itemids - return new file_info_module($this, $course, $cm, $context, $areas); - - } else if (!array_key_exists($filearea, $areas)) { - return null; - - } else { - $fileinfofunction = $modname.'_get_file_info'; - if (function_exists($fileinfofunction)) { - return $fileinfofunction($this, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename); - } - } - - return null; - } -} Index: lib/file/stored_file.php =================================================================== RCS file: lib/file/stored_file.php diff -N lib/file/stored_file.php --- lib/file/stored_file.php 28 May 2010 07:29:12 -0000 1.20 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,460 +0,0 @@ -. - - -/** - * Definition of a class stored_file. - * - * @package moodlecore - * @subpackage file-storage - * @copyright 2008 Petr Skoda {@link http://skodak.org} - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -require_once("$CFG->libdir/file/stored_file.php"); - -/** - * Class representing local files stored in a sha1 file pool. - * - * Since Moodle 2.0 file contents are stored in sha1 pool and - * all other file information is stored in new "files" database table. - * - * @copyright 2008 Petr Skoda {@link http://skodak.org} - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * @since Moodle 2.0 - */ -class stored_file { - /** @var file_storage file storage pool instance */ - private $fs; - /** @var object record from the files table */ - private $file_record; - - /** - * Constructor, this constructor should be called ONLY from the file_storage class! - * - * @param file_storage $fs file storage instance - * @param object $file_record description of file - */ - public function __construct(file_storage $fs, stdClass $file_record) { - $this->fs = $fs; - $this->file_record = clone($file_record); // prevent modifications - } - - /** - * Is this a directory? - * - * Directories are only emulated, internally they are stored as empty - * files with a "." instead of name - this means empty directory contains - * exactly one empty file with name dot. - * - * @return bool true means directory, false means file - */ - public function is_directory() { - return ($this->file_record->filename === '.'); - } - - /** - * Delete file from files table. - * - * The content of files stored in sha1 pool is reclaimed - * later - the occupied disk space is reclaimed much later. - * - * @return bool always true or exception if error occurred - */ - public function delete() { - global $DB; - $DB->delete_records('files', array('id'=>$this->file_record->id)); - // moves pool file to trash if content not needed any more - $this->fs->deleted_file_cleanup($this->file_record->contenthash); - return true; // BC only - } - - /** - * Protected - developers must not gain direct access to this function. - * - * NOTE: do not make this public, we must not modify or delete the pool files directly! ;-) - * - * @return string full path to pool file with file content - **/ - protected function get_content_file_location() { - $filedir = $this->fs->get_filedir(); - $contenthash = $this->file_record->contenthash; - $l1 = $contenthash[0].$contenthash[1]; - $l2 = $contenthash[2].$contenthash[3]; - $l3 = $contenthash[4].$contenthash[5]; - return "$filedir/$l1/$l2/$l3/$contenthash"; - } - - /** - * adds this file path to a curl request (POST only) - * - * @param curl $curlrequest the curl request object - * @param string $key what key to use in the POST request - * @return void - */ - public function add_to_curl_request(&$curlrequest, $key) { - $curlrequest->_tmp_file_post_params[$key] = '@' . $this->get_content_file_location(); - } - - /** - * Returns file handle - read only mode, no writing allowed into pool files! - * - * When you want to modify a file, create a new file and delete the old one. - * - * @return resource file handle - */ - public function get_content_file_handle() { - $path = $this->get_content_file_location(); - if (!is_readable($path)) { - if (!$this->fs->try_content_recovery($this) or !is_readable($path)) { - throw new file_exception('storedfilecannotread'); - } - } - return fopen($path, 'rb'); //binary reading only!! - } - - /** - * Dumps file content to page. - * - * @return void - */ - public function readfile() { - $path = $this->get_content_file_location(); - if (!is_readable($path)) { - if (!$this->fs->try_content_recovery($this) or !is_readable($path)) { - throw new file_exception('storedfilecannotread'); - } - } - readfile($path); - } - - /** - * Returns file content as string. - * - * @return string content - */ - public function get_content() { - $path = $this->get_content_file_location(); - if (!is_readable($path)) { - if (!$this->fs->try_content_recovery($this) or !is_readable($path)) { - throw new file_exception('storedfilecannotread'); - } - } - return file_get_contents($this->get_content_file_location()); - } - - /** - * Copy content of file to given pathname. - * - * @param string $pathname real path to the new file - * @return bool success - */ - public function copy_content_to($pathname) { - $path = $this->get_content_file_location(); - if (!is_readable($path)) { - if (!$this->fs->try_content_recovery($this) or !is_readable($path)) { - throw new file_exception('storedfilecannotread'); - } - } - return copy($path, $pathname); - } - - /** - * List contents of archive. - * - * @param file_packer $file_packer - * @return array of file infos - */ - public function list_files(file_packer $packer) { - $archivefile = $this->get_content_file_location(); - return $packer->list_files($archivefile); - } - - /** - * Extract file to given file path (real OS filesystem), existing files are overwritten. - * - * @param file_packer $file_packer - * @param string $pathname target directory - * @return array|bool list of processed files; false if error - */ - public function extract_to_pathname(file_packer $packer, $pathname) { - $archivefile = $this->get_content_file_location(); - return $packer->extract_to_pathname($archivefile, $pathname); - } - - /** - * Extract file to given file path (real OS filesystem), existing files are overwritten. - * - * @param file_packer $file_packer - * @param int $contextid - * @param string $filearea - * @param int $itemid - * @param string $pathbase - * @param int $userid - * @return array|bool list of processed files; false if error - */ - public function extract_to_storage(file_packer $packer, $contextid, $filearea, $itemid, $pathbase, $userid = NULL) { - $archivefile = $this->get_content_file_location(); - return $packer->extract_to_storage($archivefile, $contextid, $filearea, $itemid, $pathbase); - } - - /** - * Add file/directory into archive. - * - * @param file_archive $filearch - * @param string $archivepath pathname in archive - * @return bool success - */ - public function archive_file(file_archive $filearch, $archivepath) { - if ($this->is_directory()) { - return $filearch->add_directory($archivepath); - } else { - $path = $this->get_content_file_location(); - if (!is_readable($path)) { - return false; - } - return $filearch->add_file_from_pathname($archivepath, $path); - } - } - - /** - * Returns information about image, - * information is determined from the file content - * @return mixed array with width, height and mimetype; false if not an image - */ - public function get_imageinfo() { - if (!$imageinfo = getimagesize($this->get_content_file_location())) { - return false; - } - $image = array('width'=>$imageinfo[0], 'height'=>$imageinfo[1], 'mimetype'=>image_type_to_mime_type($imageinfo[2])); - if (empty($image['width']) or empty($image['height']) or empty($image['mimetype'])) { - // gd can not parse it, sorry - return false; - } - return $image; - } - - /** - * Verifies the file is a valid web image - gif, png and jpeg only. - * - * It should be ok to serve this image from server without any other security workarounds. - * - * @return bool true if file ok - */ - public function is_valid_image() { - $mimetype = $this->get_mimetype(); - if ($mimetype !== 'image/gif' and $mimetype !== 'image/jpeg' and $mimetype !== 'image/png') { - return false; - } - if (!$info = $this->get_imageinfo()) { - return false; - } - if ($info['mimetype'] !== $mimetype) { - return false; - } - // ok, GD likes this image - return true; - } - - /** - * Returns parent directory, creates missing parents if needed. - * - * @return stored_file - */ - public function get_parent_directory() { - if ($this->file_record->filepath === '/' and $this->file_record->filename === '.') { - //root dir does not have parent - return null; - } - - if ($this->file_record->filename !== '.') { - return $this->fs->create_directory($this->file_record->contextid, $this->file_record->filearea, $this->file_record->itemid, $this->file_record->filepath); - } - - $filepath = $this->file_record->filepath; - $filepath = trim($filepath, '/'); - $dirs = explode('/', $filepath); - array_pop($dirs); - $filepath = implode('/', $dirs); - $filepath = ($filepath === '') ? '/' : "/$filepath/"; - - return $this->fs->create_directory($this->file_record->contextid, $this->file_record->filearea, $this->file_record->itemid, $filepath); - } - - /** - * Returns context id of the file- - * - * @return int context id - */ - public function get_contextid() { - return $this->file_record->contextid; - } - - /** - * Returns file area name, the areas do not have to be unique, - * but usually have form component_typeofarea such as forum_attachments. - * - * @return string - */ - public function get_filearea() { - return $this->file_record->filearea; - } - - /** - * Returns returns item id of file. - * - * @return int - */ - public function get_itemid() { - return $this->file_record->itemid; - } - - /** - * Returns file path - starts and ends with /, \ are not allowed. - * - * @return string - */ - public function get_filepath() { - return $this->file_record->filepath; - } - - /** - * Returns file name or '.' in case of directories. - * - * @return string - */ - public function get_filename() { - return $this->file_record->filename; - } - - /** - * Returns id of user who created the file. - * - * @return int - */ - public function get_userid() { - return $this->file_record->userid; - } - - /** - * Returns the size of file in bytes. - * - * @return int bytes - */ - public function get_filesize() { - return $this->file_record->filesize; - } - - /** - * Returns mime type of file. - * - * @return string - */ - public function get_mimetype() { - return $this->file_record->mimetype; - } - - /** - * Returns unix timestamp of file creation date. - * - * @return int - */ - public function get_timecreated() { - return $this->file_record->timecreated; - } - - /** - * Returns unix timestamp of last file modification. - * - * @return int - */ - public function get_timemodified() { - return $this->file_record->timemodified; - } - - /** - * Returns file status flag. - * - * @return int 0 means file OK, anything else is a problem and file can not be used - */ - public function get_status() { - return $this->file_record->status; - } - - /** - * Returns file id. - * - * @return int - */ - public function get_id() { - return $this->file_record->id; - } - - /** - * Returns sha1 hash of file content. - * - * @return string - */ - public function get_contenthash() { - return $this->file_record->contenthash; - } - - /** - * Returns sha1 hash of all file path components sha1("contextid/filearea/itemid/dir/dir/filename.ext"). - * - * @return string - */ - public function get_pathnamehash() { - return $this->file_record->pathnamehash; - } - - /** - * Returns the license type of the file, it is a short name referred from license table. - * - * @return string - */ - public function get_license() { - return $this->file_record->license; - } - - /** - * Returns the author name of the file. - * - * @return string - */ - public function get_author() { - return $this->file_record->license; - } - - /** - * Returns the source of the file, usually it is a url. - * - * @return string - */ - public function get_source() { - return $this->file_record->source; - } - - /** - * Returns the sort order of file - * - * @return int - */ - public function get_sortorder() { - return $this->file_record->sortorder; - } -} Index: mod/assignment/db/upgrade.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/assignment/db/upgrade.php,v retrieving revision 1.33 diff -u -r1.33 upgrade.php --- mod/assignment/db/upgrade.php 1 Nov 2009 13:21:57 -0000 1.33 +++ mod/assignment/db/upgrade.php 28 Jun 2010 22:32:35 -0000 @@ -69,7 +69,7 @@ // migrate submitted files first $path = $basepath; - $filearea = 'assignment_submission'; + $filearea = 'submission'; $items = new DirectoryIterator($path); foreach ($items as $item) { if (!$item->isFile()) { @@ -83,8 +83,8 @@ if ($filename === '') { continue; } - if (!$fs->file_exists($context->id, $filearea, $submission->userid, '/', $filename)) { - $file_record = array('contextid'=>$context->id, 'filearea'=>$filearea, 'itemid'=>$submission->userid, 'filepath'=>'/', 'filename'=>$filename, 'userid'=>$submission->userid); + if (!$fs->file_exists($context->id, 'mod_assignmnet', $filearea, $submission->userid, '/', $filename)) { + $file_record = array('contextid'=>$context->id, 'component'=>'mod_assignment', 'filearea'=>$filearea, 'itemid'=>$submission->userid, 'filepath'=>'/', 'filename'=>$filename, 'userid'=>$submission->userid); if ($fs->create_file_from_pathname($file_record, $path.$item->getFilename())) { unlink($path.$item->getFilename()); } @@ -95,7 +95,7 @@ // migrate teacher response files $path = $basepath.'responses/'; if (file_exists($path)) { - $filearea = 'assignment_response'; + $filearea = 'response'; $items = new DirectoryIterator($path); foreach ($items as $item) { if (!$item->isFile()) { @@ -105,8 +105,8 @@ if ($filename === '') { continue; } - if (!$fs->file_exists($context->id, $filearea, $submission->userid, '/', $filename)) { - $file_record = array('contextid'=>$context->id, 'filearea'=>$filearea, 'itemid'=>$submission->userid, 'filepath'=>'/', 'filename'=>$filename, + if (!$fs->file_exists($context->id, 'mod_assignment', $filearea, $submission->userid, '/', $filename)) { + $file_record = array('contextid'=>$context->id, 'component'=>'mod_assignment', 'filearea'=>$filearea, 'itemid'=>$submission->userid, 'filepath'=>'/', 'filename'=>$filename, 'timecreated'=>$item->getCTime(), 'timemodified'=>$item->getMTime()); if ($submission->teacher) { $file_record['userid'] = $submission->teacher; Index: mod/assignment/type/upload/assignment.class.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/assignment/type/upload/assignment.class.php,v retrieving revision 1.109 diff -u -r1.109 assignment.class.php --- mod/assignment/type/upload/assignment.class.php 6 May 2010 21:28:00 -0000 1.109 +++ mod/assignment/type/upload/assignment.class.php 28 Jun 2010 22:32:36 -0000 @@ -283,15 +283,14 @@ } $fs = get_file_storage(); - $browser = get_file_browser(); - if ($files = $fs->get_area_files($this->context->id, 'assignment_submission', $userid, "timemodified", false)) { + if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $userid, "timemodified", false)) { foreach ($files as $file) { $filename = $file->get_filename(); $found = true; $mimetype = $file->get_mimetype(); - $path = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/assignment_submission/'.$userid.'/'.$filename); + $path = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/mod_assignment/submission/'.$userid.'/'.$filename); $output .= ''.$mimetype.''.s($filename).' '; } @@ -342,14 +341,13 @@ } $fs = get_file_storage(); - $browser = get_file_browser(); - if ($files = $fs->get_area_files($this->context->id, 'assignment_submission', $userid, "timemodified", false)) { + if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $userid, "timemodified", false)) { $button = new portfolio_add_button(); foreach ($files as $file) { $filename = $file->get_filename(); $mimetype = $file->get_mimetype(); - $path = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/assignment_submission/'.$userid.'/'.$filename); + $path = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/mod_assignment/submission/'.$userid.'/'.$filename); $output .= ''.$mimetype.''.s($filename).''; if ($candelete) { @@ -403,14 +401,13 @@ $strdelete = get_string('delete'); $fs = get_file_storage(); - $browser = get_file_browser(); - if ($files = $fs->get_area_files($this->context->id, 'assignment_response', $userid, "timemodified", false)) { + if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'response', $userid, "timemodified", false)) { foreach ($files as $file) { $filename = $file->get_filename(); $found = true; $mimetype = $file->get_mimetype(); - $path = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/assignment_response/'.$userid.'/'.$filename); + $path = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/mod_assignment/response/'.$userid.'/'.$filename); $output .= ''.$mimetype.''.$filename.''; @@ -539,8 +536,8 @@ $fs = get_file_storage(); $filename = $mform->get_new_filename('newfile'); if ($filename !== false) { - if (!$fs->file_exists($this->context->id, 'assignment_response', $userid, '/', $filename)) { - if ($file = $mform->save_stored_file('newfile', $this->context->id, 'assignment_response', $userid, '/', $filename, false, $USER->id)) { + if (!$fs->file_exists($this->context->id, 'mod_assignment', 'response', $userid, '/', $filename)) { + if ($file = $mform->save_stored_file('newfile', $this->context->id, 'mod_assignment', 'response', $userid, '/', $filename, false, $USER->id)) { redirect($returnurl); } } @@ -575,8 +572,8 @@ $fs = get_file_storage(); $filename = $mform->get_new_filename('newfile'); if ($filename !== false) { - if (!$fs->file_exists($this->context->id, 'assignment_submission', $USER->id, '/', $filename)) { - if ($file = $mform->save_stored_file('newfile', $this->context->id, 'assignment_submission', $USER->id, '/', $filename, false, $USER->id)) { + if (!$fs->file_exists($this->context->id, 'mod_assignment', 'submission', $USER->id, '/', $filename)) { + if ($file = $mform->save_stored_file('newfile', $this->context->id, 'mod_assignment', 'submission', $USER->id, '/', $filename, false, $USER->id)) { $submission = $this->get_submission($USER->id, true); //create new submission if needed $submission->timemodified = time(); if ($DB->update_record('assignment_submissions', $submission)) { @@ -619,8 +616,8 @@ require_login($this->course, false, $this->cm); $userid = (int)array_shift($args); - $relativepath = '/'.implode('/', $args); - $fullpath = $this->context->id.$filearea.$userid.$relativepath; + $relativepath = implode('/', $args); + $fullpath = "/{$this->context->id}/mod_assignment/$filearea/$userid/$relativepath"; $fs = get_file_storage(); @@ -628,12 +625,12 @@ return false; } - if ($filearea === 'assignment_submission') { + if ($filearea === 'submission') { if ($USER->id != $userid and !has_capability('mod/assignment:grade', $this->context)) { return false; } - } else if ($filearea === 'assignment_response') { + } else if ($filearea === 'response') { if ($USER->id != $userid and !has_capability('mod/assignment:grade', $this->context)) { return false; } @@ -801,7 +798,7 @@ } $fs = get_file_storage(); - if ($file = $fs->get_file($this->context->id, 'assignment_submission', $userid, '/', $file)) { + if ($file = $fs->get_file($this->context->id, 'mod_assignment', 'submission', $userid, '/', $file)) { if ($file->delete()) { redirect($returnurl); } @@ -867,7 +864,7 @@ } $fs = get_file_storage(); - if ($file = $fs->get_file($this->context->id, 'assignment_submission', $userid, '/', $file)) { + if ($file = $fs->get_file($this->context->id, 'mod_assignment', 'submission', $userid, '/', $file)) { if ($file->delete()) { $submission->timemodified = time(); if ($DB->update_record('assignment_submissions', $submission)) { @@ -1020,7 +1017,7 @@ function count_responsefiles($userid) { $fs = get_file_storage(); - $files = $fs->get_area_files($this->context->id, 'assignment_response', $userid, "id", false); + $files = $fs->get_area_files($this->context->id, 'mod_assignment', 'response', $userid, "id", false); return count($files); } @@ -1099,7 +1096,7 @@ // Check if the user has uploaded any files, if so we can add some more stuff to the settings nav if ($submission && has_capability('mod/assignment:submit', $this->context) && $this->count_user_files($USER->id)) { $fs = get_file_storage(); - if ($files = $fs->get_area_files($this->context->id, 'assignment_submission', $USER->id, "timemodified", false)) { + if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $USER->id, "timemodified", false)) { if (!$this->drafts_tracked() or !$this->isopen() or $this->is_finalized($submission)) { $filenode = $node->add(get_string('submission', 'assignment')); } else { @@ -1108,7 +1105,7 @@ foreach ($files as $file) { $filename = $file->get_filename(); $mimetype = $file->get_mimetype(); - $link = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/assignment_submission/'.$USER->id.'/'.$filename); + $link = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/mod_assignment/submission/'.$USER->id.'/'.$filename); $filenode->add($filename, $link, navigation_node::TYPE_SETTING, null, null, new pix_icon(file_mimetype_icon($mimetype),'')); } } @@ -1149,8 +1146,8 @@ if ((groups_is_member($groupid,$a_userid)or !$groupmode or !$groupid)) { $a_assignid = $submission->assignment; //get name of this assignment for use in the file names. $a_user = $DB->get_record("user", array("id"=>$a_userid),'id,username,firstname,lastname'); //get user firstname/lastname - - $files = $fs->get_area_files($this->context->id, 'assignment_submission', $a_userid, "timemodified", false); + + $files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $a_userid, "timemodified", false); foreach ($files as $file) { //get files new name. $fileforzipname = $a_user->username . "_" . $filenewname . "_" . $file->get_filename(); Index: lib/packer/zip_packer.php =================================================================== RCS file: lib/packer/zip_packer.php diff -N lib/packer/zip_packer.php --- lib/packer/zip_packer.php 1 Nov 2009 16:48:46 -0000 1.10 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,418 +0,0 @@ -. - - -/** - * Implementation of zip packer. - * - * @package moodlecore - * @subpackage file-packer - * @copyright 2008 Petr Skoda (http://skodak.org) - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -require_once("$CFG->libdir/packer/file_packer.php"); -require_once("$CFG->libdir/packer/zip_archive.php"); - -/** - * Utility class - handles all zipping and unzipping operations. - */ -class zip_packer extends file_packer { - - /** - * Zip files and store the result in file storage - * @param array $files array with full zip paths (including directory information) as keys (archivepath=>ospathname or archivepath/subdir=>stored_file) - * @param int $contextid - * @param string $filearea - * @param int $itemid - * @param string $filepath - * @param string $filename - * @return mixed false if error stored file instance if ok - */ - public function archive_to_storage($files, $contextid, $filearea, $itemid, $filepath, $filename, $userid=null) { - global $CFG; - - $fs = get_file_storage(); - - check_dir_exists($CFG->dataroot.'/temp/zip', true, true); - $tmpfile = tempnam($CFG->dataroot.'/temp/zip', 'zipstor'); - - if ($result = $this->archive_to_pathname($files, $tmpfile)) { - if ($file = $fs->get_file($contextid, $filearea, $itemid, $filepath, $filename)) { - if (!$file->delete()) { - @unlink($tmpfile); - return false; - } - } - $file_record = new object(); - $file_record->contextid = $contextid; - $file_record->filearea = $filearea; - $file_record->itemid = $itemid; - $file_record->filepath = $filepath; - $file_record->filename = $filename; - $file_record->userid = $userid; - $file_record->mimetype = 'application/zip'; - - $result = $fs->create_file_from_pathname($file_record, $tmpfile); - } - @unlink($tmpfile); - return $result; - } - - /** - * Zip files and store the result in os file - * @param array $files array with zip paths as keys (archivepath=>ospathname or archivepath=>stored_file) - * @param string $archivefile path to target zip file - * @return bool success - */ - public function archive_to_pathname($files, $archivefile) { - global $CFG; - - if (!is_array($files)) { - return false; - } - - $ziparch = new zip_archive(); - if (!$ziparch->open($archivefile, file_archive::OVERWRITE)) { - return false; - } - - foreach ($files as $archivepath => $file) { - $archivepath = trim($archivepath, '/'); - - if (is_null($file)) { - // empty directories have null as content - $ziparch->add_directory($archivepath.'/'); - - } else if (is_string($file)) { - $this->archive_pathname($ziparch, $archivepath, $file); - - } else { - $this->archive_stored($ziparch, $archivepath, $file); - } - } - - return $ziparch->close(); - } - - private function archive_stored($ziparch, $archivepath, $file) { - $file->archive_file($ziparch, $archivepath); - - if (!$file->is_directory()) { - return; - } - - $baselength = strlen($file->get_filepath()); - $fs = get_file_storage(); - $files = $fs->get_directory_files($file->get_contextid(), $file->get_filearea(), $file->get_itemid(), - $file->get_filepath(), true, true); - foreach ($files as $file) { - $path = $file->get_filepath(); - $path = substr($path, $baselength); - $path = $archivepath.'/'.$path; - if (!$file->is_directory()) { - $path = $path.$file->get_filename(); - } - $file->archive_file($ziparch, $path); - } - } - - private function archive_pathname($ziparch, $archivepath, $file) { - if (!file_exists($file)) { - return; - } - - if (is_file($file)) { - if (!is_readable($file)) { - return; - } - $ziparch->add_file_from_pathname($archivepath, $file); - return; - } - if (is_dir($file)) { - if ($archivepath !== '') { - $ziparch->add_directory($archivepath); - } - $files = new DirectoryIterator($file); - foreach ($files as $file) { - if ($file->isDot()) { - continue; - } - $newpath = $archivepath.'/'.$file->getFilename(); - $this->archive_pathname($ziparch, $newpath, $file->getPathname()); - } - unset($files); //release file handles - return; - } - } - - /** - * Unzip file to given file path (real OS filesystem), existing files are overwrited - * @param mixed $archivefile full pathname of zip file or stored_file instance - * @param string $pathname target directory - * @return mixed list of processed files; false if error - */ - public function extract_to_pathname($archivefile, $pathname) { - global $CFG; - - if (!is_string($archivefile)) { - return $archivefile->extract_to_pathname($this, $pathname); - } - - $processed = array(); - - $pathname = rtrim($pathname, '/'); - if (!is_readable($archivefile)) { - return false; - } - $ziparch = new zip_archive(); - if (!$ziparch->open($archivefile, file_archive::OPEN)) { - return false; - } - - foreach ($ziparch as $info) { - $size = $info->size; - $name = $info->pathname; - - if ($name === '' or array_key_exists($name, $processed)) { - //probably filename collisions caused by filename cleaning/conversion - continue; - } - - if ($info->is_directory) { - $newdir = "$pathname/$name"; - // directory - if (is_file($newdir) and !unlink($newdir)) { - $processed[$name] = 'Can not create directory, file already exists'; // TODO: localise - continue; - } - if (is_dir($newdir)) { - //dir already there - $processed[$name] = true; - } else { - if (mkdir($newdir, $CFG->directorypermissions, true)) { - $processed[$name] = true; - } else { - $processed[$name] = 'Can not create directory'; // TODO: localise - } - } - continue; - } - - $parts = explode('/', trim($name, '/')); - $filename = array_pop($parts); - $newdir = rtrim($pathname.'/'.implode('/', $parts), '/'); - - if (!is_dir($newdir)) { - if (!mkdir($newdir, $CFG->directorypermissions, true)) { - $processed[$name] = 'Can not create directory'; // TODO: localise - continue; - } - } - - $newfile = "$newdir/$filename"; - if (!$fp = fopen($newfile, 'wb')) { - $processed[$name] = 'Can not write target file'; // TODO: localise - continue; - } - if (!$fz = $ziparch->get_stream($info->index)) { - $processed[$name] = 'Can not read file from zip archive'; // TODO: localise - fclose($fp); - continue; - } - - while (!feof($fz)) { - $content = fread($fz, 262143); - fwrite($fp, $content); - } - fclose($fz); - fclose($fp); - if (filesize($newfile) !== $size) { - $processed[$name] = 'Unknown error during zip extraction'; // TODO: localise - // something went wrong :-( - @unlink($newfile); - continue; - } - $processed[$name] = true; - } - $ziparch->close(); - return $processed; - } - - /** - * Unzip file to given file path (real OS filesystem), existing files are overwrited - * @param mixed $archivefile full pathname of zip file or stored_file instance - * @param int $contextid - * @param string $filearea - * @param int $itemid - * @param string $filepath - * @return mixed list of processed files; false if error - */ - public function extract_to_storage($archivefile, $contextid, $filearea, $itemid, $pathbase, $userid=null) { - global $CFG; - - if (!is_string($archivefile)) { - return $archivefile->extract_to_pathname($this, $contextid, $filearea, $itemid, $pathbase, $userid); - } - - check_dir_exists($CFG->dataroot.'/temp/zip', true, true); - - $pathbase = trim($pathbase, '/'); - $pathbase = ($pathbase === '') ? '/' : '/'.$pathbase.'/'; - $fs = get_file_storage(); - - $processed = array(); - - $ziparch = new zip_archive(); - if (!$ziparch->open($archivefile, file_archive::OPEN)) { - return false; - } - - foreach ($ziparch as $info) { - $size = $info->size; - $name = $info->pathname; - - if ($name === '' or array_key_exists($name, $processed)) { - //probably filename collisions caused by filename cleaning/conversion - continue; - } - - if ($info->is_directory) { - $newfilepath = $pathbase.$name.'/'; - $fs->create_directory($contextid, $filearea, $itemid, $newfilepath, $userid); - $processed[$name] = true; - continue; - } - - $parts = explode('/', trim($name, '/')); - $filename = array_pop($parts); - $filepath = $pathbase; - if ($parts) { - $filepath .= implode('/', $parts).'/'; - } - - if ($size < 2097151) { - // small file - if (!$fz = $ziparch->get_stream($info->index)) { - $processed[$name] = 'Can not read file from zip archive'; // TODO: localise - continue; - } - $content = ''; - while (!feof($fz)) { - $content .= fread($fz, 262143); - } - fclose($fz); - if (strlen($content) !== $size) { - $processed[$name] = 'Unknown error during zip extraction'; // TODO: localise - // something went wrong :-( - unset($content); - continue; - } - - if ($file = $fs->get_file($contextid, $filearea, $itemid, $filepath, $filename)) { - if (!$file->delete()) { - $processed[$name] = 'Can not delete existing file'; // TODO: localise - continue; - } - } - $file_record = new object(); - $file_record->contextid = $contextid; - $file_record->filearea = $filearea; - $file_record->itemid = $itemid; - $file_record->filepath = $filepath; - $file_record->filename = $filename; - $file_record->userid = $userid; - if ($fs->create_file_from_string($file_record, $content)) { - $processed[$name] = true; - } else { - $processed[$name] = 'Unknown error during zip extraction'; // TODO: localise - } - unset($content); - continue; - - } else { - // large file, would not fit into memory :-( - $tmpfile = tempnam($CFG->dataroot.'/temp/zip', 'unzip'); - if (!$fp = fopen($tmpfile, 'wb')) { - @unlink($tmpfile); - $processed[$name] = 'Can not write temp file'; // TODO: localise - continue; - } - if (!$fz = $ziparch->get_stream($info->index)) { - @unlink($tmpfile); - $processed[$name] = 'Can not read file from zip archive'; // TODO: localise - continue; - } - while (!feof($fz)) { - $content = fread($fz, 262143); - fwrite($fp, $content); - } - fclose($fz); - fclose($fp); - if (filesize($tmpfile) !== $size) { - $processed[$name] = 'Unknown error during zip extraction'; // TODO: localise - // something went wrong :-( - @unlink($tmpfile); - continue; - } - - if ($file = $fs->get_file($contextid, $filearea, $itemid, $filepath, $filename)) { - if (!$file->delete()) { - @unlink($tmpfile); - $processed[$name] = 'Can not delete existing file'; // TODO: localise - continue; - } - } - $file_record = new object(); - $file_record->contextid = $contextid; - $file_record->filearea = $filearea; - $file_record->itemid = $itemid; - $file_record->filepath = $filepath; - $file_record->filename = $filename; - $file_record->userid = $userid; - if ($fs->create_file_from_pathname($file_record, $tmpfile)) { - $processed[$name] = true; - } else { - $processed[$name] = 'Unknown error during zip extraction'; // TODO: localise - } - @unlink($tmpfile); - continue; - } - } - $ziparch->close(); - return $processed; - } - - /** - * Returns array of info about all files in archive - * @return array of file infos - */ - public function list_files($archivefile) { - if (!is_string($archivefile)) { - return $archivefile->list_files(); - } - - $ziparch = new zip_archive(); - if (!$ziparch->open($archivefile, file_archive::OPEN)) { - return false; - } - $list = $ziparch->list_files(); - $ziparch->close(); - return $list; - } - -} Index: lib/packer/zip_archive.php =================================================================== RCS file: lib/packer/zip_archive.php diff -N lib/packer/zip_archive.php --- lib/packer/zip_archive.php 21 May 2010 19:27:20 -0000 1.5 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,331 +0,0 @@ -. - - -/** - * Implementation of zip file archive. - * - * @package moodlecore - * @subpackage file-packer - * @copyright 2008 Petr Skoda (http://skodak.org) - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -require_once("$CFG->libdir/packer/file_archive.php"); - -class zip_archive extends file_archive { - - /** Pathname of archive */ - protected $archivepathname = null; - - /** Used memory tracking */ - protected $usedmem = 0; - - /** Iteration position */ - protected $pos = 0; - - /** TipArchive instance */ - protected $za; - - /** - * Open or create archive (depending on $mode) - * @param string $archivepathname - * @param int $mode OPEN, CREATE or OVERWRITE constant - * @param string $encoding archive local paths encoding - * @return bool success - */ - public function open($archivepathname, $mode=file_archive::CREATE, $encoding='utf-8') { - $this->close(); - - $this->usedmem = 0; - $this->pos = 0; - - $this->za = new ZipArchive(); - - switch($mode) { - case file_archive::OPEN: $flags = 0; break; - case file_archive::OVERWRITE: $flags = ZIPARCHIVE::OVERWRITE; break; - case file_archive::CREATE: - default : $flags = ZIPARCHIVE::CREATE; break; - } - - $result = $this->za->open($archivepathname, $flags); - - if ($result === true) { - $this->encoding = $encoding; - if (file_exists($archivepathname)) { - $this->archivepathname = realpath($archivepathname); - } else { - $this->archivepathname = $archivepathname; - } - return true; - - } else { - $this->za = null; - $this->archivepathname = null; - $this->encooding = 'utf-8'; - // TODO: maybe we should return some error info - return false; - } - } - - /** - * Close archive - * @return bool success - */ - public function close() { - if (!isset($this->za)) { - return false; - } - - $res = $this->za->close(); - $this->za = null; - - return $res; - } - - /** - * Returns file stream for reading of content - * @param int $index of file - * @return stream or false if error - */ - public function get_stream($index) { - if (!isset($this->za)) { - return false; - } - - $name = $this->za->getNameIndex($index); - if ($name === false) { - return false; - } - - return $this->za->getStream($name); - } - - /** - * Returns file information - * @param int $index of file - * @return info object or false if error - */ - public function get_info($index) { - if (!isset($this->za)) { - return false; - } - - if ($index < 0 or $index >=$this->count()) { - return false; - } - - $result = $this->za->statIndex($index); - - if ($result === false) { - return false; - } - - $info = new object(); - $info->index = $index; - $info->original_pathname = $result['name']; - $info->pathname = $this->unmangle_pathname($result['name']); - $info->mtime = (int)$result['mtime']; - - if ($info->pathname[strlen($info->pathname)-1] === '/') { - $info->is_directory = true; - $info->size = 0; - } else { - $info->is_directory = false; - $info->size = (int)$result['size']; - } - - return $info; - } - - /** - * Returns array of info about all files in archive - * @return array of file infos - */ - public function list_files() { - if (!isset($this->za)) { - return false; - } - - $infos = array(); - - for ($i=0; $i<$this->count(); $i++) { - $info = $this->get_info($i); - if ($info === false) { - continue; - } - $infos[$i] = $info; - } - - return $infos; - } - - /** - * Returns number of files in archive - * @return int number of files - */ - public function count() { - if (!isset($this->za)) { - return false; - } - - return $this->za->numFiles; - } - - /** - * Add file into archive - * @param string $localname name of file in archive - * @param string $pathname location of file - * @return bool success - */ - public function add_file_from_pathname($localname, $pathname) { - if (!isset($this->za)) { - return false; - } - - if ($this->archivepathname === realpath($pathname)) { - // do not add self into archive - return false; - } - - if (is_null($localname)) { - $localname = clean_param($pathname, PARAM_PATH); - } - $localname = trim($localname, '/'); // no leading slashes in archives - $localname = $this->mangle_pathname($localname); - - if ($localname === '') { - //sorry - conversion failed badly - return false; - } - - if (!check_php_version('5.2.8')) { - // workaround for open file handles problem, ZipArchive uses file locking in order to prevent file modifications before the close() (strange, eh?) - if ($this->count() > 0 and $this->count() % 500 === 0) { - $this->close(); - $res = $this->open($this->archivepathname, file_archive::OPEN, $this->encoding); - if ($res !== true) { - print_error('cannotopenzip'); //TODO ?? - } - } - } - - return $this->za->addFile($pathname, $localname); - } - - /** - * Add content of string into archive - * @param string $localname name of file in archive - * @param string $contents - * @return bool success - */ - public function add_file_from_string($localname, $contents) { - if (!isset($this->za)) { - return false; - } - - $localname = trim($localname, '/'); // no leading slashes in archives - $localname = $this->mangle_pathname($localname); - - if ($localname === '') { - //sorry - conversion failed badly - return false; - } - - if ($this->usedmem > 2097151) { - /// this prevents running out of memory when adding many large files using strings - $this->close(); - $res = $this->open($this->archivepathname, file_archive::OPEN, $this->encoding); - if ($res !== true) { - print_error('cannotopenzip'); //TODO ?? - } - } - $this->usedmem += strlen($contents); - - return $this->za->addFromString($localname, $contents); - - } - - /** - * Add empty directory into archive - * @param string $local - * @return bool success - */ - public function add_directory($localname) { - if (!isset($this->za)) { - return false; - } - $localname = ltrim($localname, '/'). '/'; - $localname = $this->mangle_pathname($localname); - - if ($localname === '/') { - //sorry - conversion failed badly - return false; - } - - return $this->za->addEmptyDir($localname); - } - - /** - * Returns current file info - * @return object - */ - public function current() { - if (!isset($this->za)) { - return false; - } - - return $this->get_info($this->pos); - } - - /** - * Returns the index of current file - * @return int current file index - */ - public function key() { - return $this->pos; - } - - /** - * Moves forward to next file - * @return void - */ - public function next() { - $this->pos++; - } - - /** - * Rewinds back to the first file - * @return void - */ - public function rewind() { - $this->pos = 0; - } - - /** - * Did we reach the end? - * @return boolean - */ - public function valid() { - if (!isset($this->za)) { - return false; - } - - return ($this->pos < $this->count()); - } -} Index: lib/packer/file_packer.php =================================================================== RCS file: lib/packer/file_packer.php diff -N lib/packer/file_packer.php --- lib/packer/file_packer.php 1 Nov 2009 16:48:46 -0000 1.7 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,77 +0,0 @@ -. - - -/** - * Abstraction of general file packer. - * - * @package moodlecore - * @subpackage file-packer - * @copyright 2008 Petr Skoda (http://skodak.org) - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -/** - * Abstract class for archiving of files. - */ -abstract class file_packer { - - /** - * Archive files and store the result in file storage - * @param array $files array with zip paths as keys (archivepath=>ospathname or archivepath=>stored_file) - * @param int $contextid - * @param string $filearea - * @param int $itemid - * @param string $filepath - * @param string $filename - * @return mixed false if error stored file instance if ok - */ - public abstract function archive_to_storage($files, $contextid, $filearea, $itemid, $filepath, $filename, $userid=null); - - /** - * Archive files and store the result in os file - * @param array $files array with zip paths as keys (archivepath=>ospathname or archivepath=>stored_file) - * @param string $archivefile path to target zip file - * @return bool success - */ - public abstract function archive_to_pathname($files, $archivefile); - - /** - * Extract file to given file path (real OS filesystem), existing files are overwrited - * @param mixed $archivefile full pathname of zip file or stored_file instance - * @param string $pathname target directory - * @return mixed list of processed files; false if error - */ - public abstract function extract_to_pathname($archivefile, $pathname); - - /** - * Extract file to given file path (real OS filesystem), existing files are overwrited - * @param mixed $archivefile full pathname of zip file or stored_file instance - * @param int $contextid - * @param string $filearea - * @param int $itemid - * @param string $filepath - * @return mixed list of processed files; false if error - */ - public abstract function extract_to_storage($archivefile, $contextid, $filearea, $itemid, $pathbase, $userid=null); - - /** - * Returns array of info about all files in archive - * @return array of file infos - */ - public abstract function list_files($archivefile); -} Index: lib/packer/file_archive.php =================================================================== RCS file: lib/packer/file_archive.php diff -N lib/packer/file_archive.php --- lib/packer/file_archive.php 21 May 2010 19:27:08 -0000 1.5 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,200 +0,0 @@ -. - - -/** - * Abstraction of general file archives. - * - * @package moodlecore - * @subpackage file-packer - * @copyright 2008 Petr Skoda (http://skodak.org) - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -abstract class file_archive implements Iterator { - - /** Open archive if exists, fail if does not exist. */ - const OPEN = 1; - - /** Open archive if exists, create if does not. */ - const CREATE = 2; - - /** Always create new archive */ - const OVERWRITE = 4; - - /** Encoding of file names - windows usually expects DOS single-byte charset*/ - protected $encoding = 'utf-8'; - - /** - * Open or create archive (depending on $mode) - * @param string $archivepathname - * @param int $mode OPEN, CREATE or OVERWRITE constant - * @param string $encoding archive local paths encoding - * @return bool success - */ - public abstract function open($archivepathname, $mode=file_archive::CREATE, $encoding='utf-8'); - - /** - * Close archive - * @return bool success - */ - public abstract function close(); - - /** - * Returns file stream for reading of content - * @param int $index of file - * @return stream or false if error - */ - public abstract function get_stream($index); - - /** - * Returns file information - * @param int $index of file - * @return info object or false if error - */ - public abstract function get_info($index); - - /** - * Returns array of info about all files in archive - * @return array of file infos - */ - public abstract function list_files(); - - /** - * Returns number of files in archive - * @return int number of files - */ - public abstract function count(); - - /** - * Add file into archive - * @param string $localname name of file in archive - * @param string $pathname location of file - * @return bool success - */ - public abstract function add_file_from_pathname($localname, $pathname); - - /** - * Add content of string into archive - * @param string $localname name of file in archive - * @param string $contents - * @return bool success - */ - public abstract function add_file_from_string($localname, $contents); - - /** - * Add empty directory into archive - * @param string $local - * @return bool success - */ - public abstract function add_directory($localname); - - /** - * Tries to convert $localname into another encoding, - * please note that it may fail really badly. - * @param strin $localname in utf-8 encoding - * @return string - */ - protected function mangle_pathname($localname) { - if ($this->encoding === 'utf-8') { - return $localname; - } - $textlib = textlib_get_instance(); - - $converted = $textlib->convert($localname, 'utf-8', $this->encoding); - $original = $textlib->convert($converted, $this->encoding, 'utf-8'); - - if ($original === $localname) { - $result = $converted; - - } else { - // try ascci conversion - $converted2 = $textlib->specialtoascii($localname); - $converted2 = $textlib->convert($converted2, 'utf-8', $this->encoding); - $original2 = $textlib->convert($converted, $this->encoding, 'utf-8'); - - if ($original2 === $localname) { - //this looks much better - $result = $converted2; - } else { - //bad luck - the file name may not be usable at all - $result = $converted; - } - } - - $result = preg_replace('/\.\.+/', '', $result); - $result = ltrim($result); // no leadin / - - if ($result === '.') { - $result = ''; - } - - return $result; - } - - /** - * Tries to convert $localname into utf-8 - * please note that it may fail really badly. - * The resulting file name is cleaned. - * - * @param strin $localname in another encoding - * @return string in utf-8 - */ - protected function unmangle_pathname($localname) { - if ($this->encoding === 'utf-8') { - return $localname; - } - $textlib = textlib_get_instance(); - - $result = $textlib->convert($localname, $this->encoding, 'utf-8'); - $result = clean_param($result, PARAM_PATH); - $result = ltrim($result); // no leadin / - - return $result; - } - - /** - * Returns current file info - * @return object - */ - //public abstract function current(); - - /** - * Returns the index of current file - * @return int current file index - */ - //public abstract function key(); - - /** - * Moves forward to next file - * @return void - */ - //public abstract function next(); - - /** - * Rewinds back to the first file - * @return void - */ - //public abstract function rewind(); - - /** - * Did we reach the end? - * @return boolean - */ - //public abstract function valid(); - -} Index: tag/locallib.php =================================================================== RCS file: /cvsroot/moodle/moodle/tag/locallib.php,v retrieving revision 1.29 diff -u -r1.29 locallib.php --- tag/locallib.php 21 Jun 2010 15:31:02 -0000 1.29 +++ tag/locallib.php 28 Jun 2010 22:33:12 -0000 @@ -115,7 +115,7 @@ if (!empty($tag_object->description)) { $options = new object(); $options->para = false; - $tag_object->description = file_rewrite_pluginfile_urls($tag_object->description, 'pluginfile.php', get_context_instance(CONTEXT_SYSTEM)->id, 'tag_description', $tag_object->id); + $tag_object->description = file_rewrite_pluginfile_urls($tag_object->description, 'pluginfile.php', get_context_instance(CONTEXT_SYSTEM)->id, 'tag', 'description', $tag_object->id); $output .= format_text($tag_object->description, $tag_object->descriptionformat, $options); } Index: tag/lib.php =================================================================== RCS file: /cvsroot/moodle/moodle/tag/lib.php,v retrieving revision 1.94 diff -u -r1.94 lib.php --- tag/lib.php 4 Nov 2009 06:14:07 -0000 1.94 +++ tag/lib.php 28 Jun 2010 22:33:12 -0000 @@ -532,7 +532,7 @@ $success &= (bool) $DB->delete_records('tag', array('id'=>$tagid)); // Delete all files associated with this tag $fs = get_file_storage(); - $files = $fs->get_area_files($context->id, 'tag_description', $tagid); + $files = $fs->get_area_files($context->id, 'tag', 'description', $tagid); foreach ($files as $file) { $file->delete(); } Index: tag/edit.php =================================================================== RCS file: /cvsroot/moodle/moodle/tag/edit.php,v retrieving revision 1.45 diff -u -r1.45 edit.php --- tag/edit.php 28 May 2010 08:05:27 -0000 1.45 +++ tag/edit.php 28 Jun 2010 22:33:11 -0000 @@ -54,7 +54,7 @@ $errorstring = ''; $editoroptions = array('maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$CFG->maxbytes, 'trusttext'=>false); -$tag = file_prepare_standard_editor($tag, 'description', $editoroptions, $systemcontext, 'tag_description', $tag->id); +$tag = file_prepare_standard_editor($tag, 'description', $editoroptions, $systemcontext, 'tag', 'description', $tag->id); $tagform = new tag_edit_form(null, compact('editoroptions')); if ( $tag->tagtype == 'official' ) { @@ -93,7 +93,7 @@ if (empty($errorstring)) { // All is OK, let's save it - $tagnew = file_postupdate_standard_editor($tagnew, 'description', $editoroptions, $systemcontext, 'tag_description', $tag->id); + $tagnew = file_postupdate_standard_editor($tagnew, 'description', $editoroptions, $systemcontext, 'tag', 'description', $tag->id); tag_description_set($tag_id, $tagnew->description, $tagnew->descriptionformat); Index: files/module.js =================================================================== RCS file: /cvsroot/moodle/moodle/files/module.js,v retrieving revision 1.2 diff -u -r1.2 module.js --- files/module.js 17 Jun 2010 04:04:45 -0000 1.2 +++ files/module.js 28 Jun 2010 22:31:57 -0000 @@ -2,7 +2,7 @@ // Author: Dongsheng Cai M.core_filetree = { y3: null, - api: M.cfg.wwwroot+'/files/files_ajax.php', + api: M.cfg.wwwroot+'/files/filebrowser_ajax.php', request: function(url, node, cb) { var api = this.api + '?action=getfiletree'; var params = []; Index: files/draftfiles.php =================================================================== RCS file: files/draftfiles.php diff -N files/draftfiles.php --- files/draftfiles.php 16 Dec 2009 21:50:51 -0000 1.16 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,206 +0,0 @@ -. - -/** - * Draft files management script used when javascript not available. - * - * @package moodlecore - * @subpackage file - * @copyright 2008 Petr Skoda (http://skodak.org) - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -require('../config.php'); -require_once($CFG->libdir.'/filelib.php'); - -$itemid = required_param('itemid', PARAM_INT); -$filepath = optional_param('filepath', '/', PARAM_PATH); -$newdirname = optional_param('newdirname', '', PARAM_FILE); -$delete = optional_param('delete', '', PARAM_PATH); -$subdirs = optional_param('subdirs', 0, PARAM_BOOL); -$maxbytes = optional_param('maxbytes', 0, PARAM_INT); - -require_login(); -if (isguestuser()) { - print_error('noguest'); -} - -if (!$context = get_context_instance(CONTEXT_USER, $USER->id)) { - print_error('invalidcontext'); -} - -$notice = ''; - -$contextid = $context->id; -$filearea = 'user_draft'; - -$browser = get_file_browser(); -$fs = get_file_storage(); - -if (!$subdirs) { - $filepath = '/'; -} - -if (!$directory = $fs->get_file($context->id, 'user_draft', $itemid, $filepath, '.')) { - $directory = new virtual_root_file($context->id, 'user_draft', $itemid); - $filepath = $directory->get_filepath(); -} -$files = $fs->get_directory_files($context->id, 'user_draft', $itemid, $directory->get_filepath()); -$parent = $directory->get_parent_directory(); - -$totalbytes = 0; -foreach ($files as $hash=>$file) { - if (!$subdirs and $file->get_filepath() !== '/') { - unset($files[$hash]); - continue; - } - $totalbytes += $file->get_filesize(); -} - -/// process actions -if ($newdirname !== '' and data_submitted() and confirm_sesskey()) { - $newdirname = $directory->get_filepath().$newdirname.'/'; - $fs->create_directory($contextid, $filearea, $itemid, $newdirname, $USER->id); - redirect('draftfiles.php?itemid='.$itemid.'&filepath='.rawurlencode($newdirname).'&subdirs='.$subdirs.'&maxbytes='.$maxbytes); -} - -if (isset($_FILES['newfile']) and data_submitted() and confirm_sesskey()) { - if (!empty($_FILES['newfile']['error'])) { - $notice = file_get_upload_error($_FILES['newfile']['error']); - } else { - $file = $_FILES['newfile']; - $newfilename = clean_param($file['name'], PARAM_FILE); - if (is_uploaded_file($_FILES['newfile']['tmp_name'])) { - if ($existingfile = $fs->get_file($contextid, $filearea, $itemid, $filepath, $newfilename)) { - $existingfile->delete(); - } - $filerecord = array('contextid'=>$contextid, 'filearea'=>$filearea, 'itemid'=>$itemid, 'filepath'=>$filepath, - 'filename'=>$newfilename, 'userid'=>$USER->id); - $newfile = $fs->create_file_from_pathname($filerecord, $_FILES['newfile']['tmp_name']); - redirect('draftfiles.php?itemid='.$itemid.'&filepath='.rawurlencode($filepath).'&subdirs='.$subdirs.'&maxbytes='.$maxbytes); - } - } -} - -if ($delete !== '' and $file = $fs->get_file($contextid, $filearea, $itemid, $filepath, $delete)) { - if (!data_submitted() or !confirm_sesskey()) { - echo $OUTPUT->header(); - echo $OUTPUT->notification(get_string('deletecheckwarning').': '.s($file->get_filepath().$file->get_filename())); - $optionsno = array('itemid'=>$itemid, 'filepath'=>$filepath, 'subdirs'=>$subdirs); - $optionsyes = array('itemid'=>$itemid, 'filepath'=>$filepath, 'delete'=>$delete, 'sesskey'=>sesskey(), 'subdirs'=>$subdirs); - echo $OUTPUT->confirm(get_string('deletecheckfiles'), new moodle_url('draftfiles.php', $optionsyes), new moodle_url('draftfiles.php', $optionsno)); - echo $OUTPUT->footer(); - die; - - } else { - $isdir = $file->is_directory(); - $file->delete(); - if ($isdir) { - redirect('draftfiles.php?itemid='.$itemid.'&filepath='.rawurlencode($parent->get_filepath()).'&subdirs='.$subdirs.'&maxbytes='.$maxbytes); - } else { - redirect('draftfiles.php?itemid='.$itemid.'&filepath='.rawurlencode($filepath).'&subdirs='.$subdirs.'&maxbytes='.$maxbytes); - } - } -} - -echo $OUTPUT->header(); - -if ($notice !== '') { - echo $OUTPUT->notification($notice); -} - -echo '
'; - -$strfolder = get_string('folder'); -$strfile = get_string('file'); -$strdownload = get_string('download'); -$strdelete = get_string('delete'); - -if ($parent) { - echo '
'; - echo ' '.get_string('parentfolder').''; - echo '
'; -} - -foreach ($files as $file) { - $filename = $file->get_filename(); - $filenameurl = rawurlencode($filename); - $filepath = $file->get_filepath(); - $filesize = $file->get_filesize(); - $filesize = $filesize ? display_size($filesize) : ''; - - $mimetype = $file->get_mimetype(); - - if ($file->is_directory()) { - if ($subdirs) { - $dirname = explode('/', trim($filepath, '/')); - $dirname = array_pop($dirname); - echo ''; - } - - } else { - $viewurl = file_encode_url("$CFG->wwwroot/draftfile.php", "/$contextid/user_draft/$itemid".$filepath.$filename, false, false); - echo ''; - } -} - -echo '
'; - -if ($maxbytes == 0 or $maxbytes > $totalbytes) { - echo '
'; - if ($maxbytes) { - echo ''; - } - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - if ($maxbytes) { - echo ' ('.get_string('maxsize', '', display_size(get_max_upload_file_size($CFG->maxbytes, $maxbytes-$totalbytes))).')'; - } else { - echo ' ('.get_string('maxsize', '', display_size(get_max_upload_file_size($CFG->maxbytes))).')'; - } - echo '
'; -} else { - //TODO: notify upload limit reached here - echo get_string('maxsize', '', display_size(get_max_upload_file_size($CFG->maxbytes, $maxbytes))); -} - -if ($subdirs) { - echo '
'; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo '
'; -} - -echo $OUTPUT->footer(); - - Index: files/files_ajax.php =================================================================== RCS file: files/files_ajax.php diff -N files/files_ajax.php --- files/files_ajax.php 22 Jun 2010 09:04:47 -0000 1.10 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,264 +0,0 @@ -. - -/** - * File manager - * - * @package moodlecore - * @subpackage file - * @copyright 2010 Dongsheng Cai - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -require('../config.php'); -require_once($CFG->libdir.'/filelib.php'); -require_once($CFG->libdir.'/adminlib.php'); - -require_login(); - -$err = new stdclass; - -if (isguestuser()) { - $err->error = get_string('noguest'); - die(json_encode($err)); -} - -if (!confirm_sesskey()) { - $err->error = get_string('invalidsesskey'); - die(json_encode($err)); -} - -$action = optional_param('action', 'list', PARAM_ALPHA); -$fileurl = optional_param('fileurl', '', PARAM_URL); -$filename = optional_param('filename', '', PARAM_FILE); -$filearea = optional_param('filearea', 'user_draft', PARAM_ALPHAEXT); -$filepath = optional_param('filepath', '/', PARAM_PATH); -$itemid = optional_param('itemid', -1, PARAM_INT); -$newfilepath = optional_param('newfilepath', '/', PARAM_PATH); -$newdirname = optional_param('newdirname', '', PARAM_FILE); -$newfilename = optional_param('newfilename', '', PARAM_FILE); - -$user_context = get_context_instance(CONTEXT_USER, $USER->id); - -switch ($action) { -// used by course file tree viewer -case 'getfiletree': - $browser = get_file_browser(); - $fs = get_file_storage(); - $url = new moodle_url($fileurl); - $params = $url->params(); - // fix empty value - foreach ($params as $key=>$value) { - if ($value==='') { - $params[$key] = null; - } - } - $fileinfo = $browser->get_file_info(get_context_instance_by_id($params['contextid']), $params['filearea'], $params['itemid'], $params['filepath']); - $children = $fileinfo->get_children(); - $tree = array(); - foreach ($children as $child) { - $filedate = $child->get_timemodified(); - $filesize = $child->get_filesize(); - $mimetype = $child->get_mimetype(); - $params = $child->get_params(); - $url = new moodle_url('/files/index.php', $params); - $fileitem = array( - 'params'=>$params, - 'filename'=>$child->get_visible_name(), - 'filedate'=>$filedate ? userdate($filedate) : '', - 'filesize'=>$filesize ? display_size($filesize) : '', - ); - if ($child->is_directory()) { - $fileitem['isdir'] = true; - $fileitem['url'] = $url->out(); - - // hide empty folder - if (!empty($params['itemid'])) { - $itemid = $params['itemid']; - } else { - $itemid = false; - } - $draftfiles = $fs->get_area_files($params['contextid'], $params['filearea'], $itemid, 'id', false); - if (count($draftfiles) == 0) { - continue; - } - } else { - $fileitem['url'] = $child->get_url(); - } - $tree[] = $fileitem; - } - echo json_encode($tree); - - break; -case 'dir': - $data = new stdclass; - file_get_user_area_folders($itemid, $filepath, $data, $filearea); - echo json_encode($data); - break; - -case 'list': - $data = file_get_user_area_files($itemid, $filepath, $filearea); - echo json_encode($data); - break; - -case 'mkdir': - $fs = get_file_storage(); - $fs->create_directory($user_context->id, $filearea, $itemid, file_correct_filepath(file_correct_filepath($filepath).$newdirname)); - $return = new stdclass; - $return->filepath = $filepath; - echo json_encode($return); - break; - -case 'delete': - $fs = get_file_storage(); - $filepath = file_correct_filepath($filepath); - $return = new stdclass; - if ($stored_file = $fs->get_file($user_context->id, $filearea, $itemid, $filepath, $filename)) { - $parent_path = $stored_file->get_parent_directory()->get_filepath(); - if($result = $stored_file->delete()) { - $return->filepath = $parent_path; - echo json_encode($return); - } else { - echo json_encode(false); - } - } else { - echo json_encode(false); - } - break; - -case 'setmainfile': - $filepath = file_correct_filepath($filepath); - // reset sort order - file_reset_sortorder($user_context->id, $filearea, $itemid); - // set main file - $return = file_set_sortorder($user_context->id, $filearea, $itemid, $filepath, $filename, 1); - echo json_encode($return); - break; - -case 'renamedir': - $fs = get_file_storage(); - $fb = get_file_browser(); - $return = new stdclass; - $fileinfo = $fb->get_file_info($user_context, $filearea, $itemid, $filepath, '.'); - if ($result = $fileinfo->delete()) { - $newdir = $fs->create_directory($user_context->id, $filearea, $itemid, file_correct_filepath($newfilename)); - $return->filepath = $newdir->get_parent_directory()->get_filepath(); - echo json_encode($return); - } else { - echo json_encode(false); - } - break; - -case 'rename': - $fb = get_file_browser(); - $file = $fb->get_file_info($user_context, $filearea, $itemid, $filepath, $filename); - $file->copy_to_storage($user_context->id, $filearea, $itemid, $filepath, $newfilename); - if ($file->delete()) { - $return = new stdclass; - $return->filepath = $filepath; - echo json_encode($return); - } else { - echo json_encode(false); - } - break; - -case 'movefile': -case 'movedir': - $fb = get_file_browser(); - $return = new stdclass; - if ($filepath != $newfilepath) { - $file = $fb->get_file_info($user_context, $filearea, $itemid, $filepath, $filename); - $file->copy_to_storage($user_context->id, $filearea, $itemid, $newfilepath, $filename); - if ($file->delete()) { - $return->filepath = $newfilepath; - } - } - if (!isset($return->filepath)) { - $return->filepath = '/'; - } - echo json_encode($return); - break; - -case 'zip': - $zipper = new zip_packer(); - $fs = get_file_storage(); - - $file = $fs->get_file($user_context->id, $filearea, $itemid, $filepath, '.'); - - $parent_path = $file->get_parent_directory()->get_filepath(); - - if ($newfile = $zipper->archive_to_storage(array($file), $user_context->id, $filearea, $itemid, $parent_path, $filepath.'.zip', $USER->id)) { - $return = new stdclass; - $return->filepath = $parent_path; - echo json_encode($return); - } else { - echo json_encode(false); - } - break; - -case 'downloaddir': - $zipper = new zip_packer(); - $fs = get_file_storage(); - $area = file_get_user_area_info($itemid, $filearea); - if ($area['filecount'] == 0) { - echo json_encode(false); - die; - } - - $stored_file = $fs->get_file($user_context->id, $filearea, $itemid, $filepath, '.'); - if ($filepath === '/') { - $parent_path = '/'; - $filename = get_string('files').'.zip'; - } else { - $parent_path = $stored_file->get_parent_directory()->get_filepath(); - $filename = trim($filepath, '/').'.zip'; - } - - // archive compressed file to an unused draft area - $newdraftitemid = file_get_unused_draft_itemid(); - if ($newfile = $zipper->archive_to_storage(array($stored_file), $user_context->id, 'user_draft', $newdraftitemid, '/', $filename, $USER->id)) { - $return = new stdclass; - $return->fileurl = $CFG->wwwroot . '/draftfile.php/' . $user_context->id .'/user_draft/'.$newdraftitemid.'/'.$filename; - $return->filepath = $parent_path; - echo json_encode($return); - } else { - echo json_encode(false); - } - break; - -case 'unzip': - $zipper = new zip_packer(); - - $fs = get_file_storage(); - - $file = $fs->get_file($user_context->id, $filearea, $itemid, $filepath, $filename); - - if ($newfile = $file->extract_to_storage($zipper, $user_context->id, $filearea, $itemid, $filepath, $USER->id)) { - $return = new stdclass; - $return->filepath = $filepath; - echo json_encode($return); - } else { - echo json_encode(false); - } - break; - -case 'upload': - break; - -default: - break; -} Index: files/externallib.php =================================================================== RCS file: /cvsroot/moodle/moodle/files/externallib.php,v retrieving revision 1.1 diff -u -r1.1 externallib.php --- files/externallib.php 16 Mar 2010 08:29:08 -0000 1.1 +++ files/externallib.php 28 Jun 2010 22:31:57 -0000 @@ -38,8 +38,9 @@ array( 'params' => new external_single_structure(array( 'contextid' => new external_value(PARAM_INT, 'context id'), - 'itemid' => new external_value(PARAM_INT, 'associated id'), + 'component' => new external_value(PARAM_TEXT, 'component'), 'filearea' => new external_value(PARAM_TEXT, 'file area'), + 'itemid' => new external_value(PARAM_INT, 'associated id'), 'filepath' => new external_value(PARAM_RAW, 'file path'), 'filename' => new external_value(PARAM_TEXT, 'file name'), ) @@ -54,7 +55,21 @@ * @return array */ public static function get_files($fileinfo) { + +throw new coding_exception('File browsing api function is not implemented yet, sorry'); + global $CFG, $USER, $OUTPUT; + if (empty($fileinfo['contextid'])) { + $context = get_system_context(); + } else { + $context = get_context_instance_by_id($fileinfo['contextid']); + } + if (empty($fileinfo['component'])) { + $fileinfo['component'] = null; + } + if (empty($fileinfo['filearea'])) { + $fileinfo['filearea'] = null; + } if (empty($fileinfo['itemid'])) { $fileinfo['itemid'] = null; } @@ -64,14 +79,6 @@ if (empty($fileinfo['filepath'])) { $fileinfo['filepath'] = null; } - if (empty($fileinfo['filearea'])) { - $fileinfo['filearea'] = null; - } - if (empty($fileinfo['contextid'])) { - $context = get_system_context(); - } else { - $context = get_context_instance_by_id($fileinfo['contextid']); - } try { $browser = get_file_browser(); @@ -79,7 +86,7 @@ $return['parents'] = array(); $return['files'] = array(); $file = $browser->get_file_info($context, null, null, null, null); - if ($file = $browser->get_file_info($context, $fileinfo['filearea'], $fileinfo['itemid'], $fileinfo['filepath'], $fileinfo['filename'])) { + if ($file = $browser->get_file_info($context, $fileinfo['component'], $fileinfo['filearea'], $fileinfo['itemid'], $fileinfo['filepath'], $fileinfo['filename'])) { $level = $file->get_parent(); while ($level) { $params = $level->get_params(); @@ -93,24 +100,28 @@ $params = $child->get_params(); if ($child->is_directory()) { $node = array( - 'filename' => $child->get_visible_name(), - 'filepath' => $params['filepath'], - 'filearea' => $params['filearea'], - 'itemid' => $params['itemid'], + //TODO: this is wrong, you need to fetch info from the child node!!!! 'contextid' => $params['contextid'], - 'url' => null, - 'isdir'=>true + 'component' => $params['component'], + 'filearea' => $params['filearea'], + 'itemid' => $params['itemid'], + 'filepath' => $params['filepath'], + 'filename' => $child->get_visible_name(), + 'url' => null, + 'isdir' =>true ); $list[] = $node; } else { $node = array( - 'filename' => $child->get_visible_name(), - 'filepath' => $params['filepath'], - 'filearea' => $params['filearea'], - 'itemid' => $params['itemid'], + //TODO: this is wrong, you need to fetch info from the child node!!!! 'contextid' => $params['contextid'], - 'url' => $child->get_url(), - 'isdir'=>false + 'component' => $params['component'], + 'filearea' => $params['filearea'], + 'itemid' => $params['itemid'], + 'filepath' => $params['filepath'], + 'filename' => $child->get_visible_name(), + 'url' => $child->get_url(), + 'isdir' => false ); $list[] = $node; } @@ -134,23 +145,25 @@ new external_single_structure( array( 'contextid' => new external_value(PARAM_INT, ''), - 'filename' => new external_value(PARAM_TEXT, ''), - 'filearea' => new external_value(PARAM_TEXT, ''), - 'filepath' => new external_value(PARAM_TEXT, ''), - 'itemid' => new external_value(PARAM_INT, '') + 'component' => new external_value(PARAM_ALPHAEXT, ''), + 'filearea' => new external_value(PARAM_ALPHAEXT, ''), + 'itemid' => new external_value(PARAM_INT, ''), + 'filepath' => new external_value(PARAM_TEXT, ''), + 'filename' => new external_value(PARAM_TEXT, ''), ) ) ), 'files' => new external_multiple_structure( new external_single_structure( array( - 'filename' => new external_value(PARAM_TEXT, ''), - 'filearea' => new external_value(PARAM_TEXT, ''), - 'filepath' => new external_value(PARAM_TEXT, ''), + 'contextid' => new external_value(PARAM_INT, ''), + 'component' => new external_value(PARAM_ALPHAEXT, ''), + 'filearea' => new external_value(PARAM_ALPHAEXT, ''), 'itemid' => new external_value(PARAM_INT, ''), + 'filepath' => new external_value(PARAM_TEXT, ''), + 'filename' => new external_value(PARAM_TEXT, ''), 'isdir' => new external_value(PARAM_BOOL, ''), 'url' => new external_value(PARAM_TEXT, ''), - 'contextid' => new external_value(PARAM_INT, '') ) ) ) @@ -167,8 +180,9 @@ array( 'params' => new external_single_structure(array( 'contextid' => new external_value(PARAM_INT, 'context id'), + 'filearea' => new external_value(PARAM_ALPHAEXT, 'file area'), + 'component' => new external_value(PARAM_ALPHAEXT, 'component'), 'itemid' => new external_value(PARAM_INT, 'associated id'), - 'filearea' => new external_value(PARAM_TEXT, 'file area'), 'filepath' => new external_value(PARAM_RAW, 'file path'), 'filename' => new external_value(PARAM_TEXT, 'file name'), 'filecontent' => new external_value(PARAM_TEXT, 'file content') @@ -215,6 +229,9 @@ file_put_contents($savedfilepath, base64_decode($fileinfo['filecontent'])); unset($fileinfo['filecontent']); + $component = $fileinfo['component']; + + //TODO: mandatory!!! if (!empty($fileinfo['filearea'])) { $filearea = $fileinfo['filearea']; } else { @@ -226,7 +243,7 @@ } else { $filepath = ''; } - + if (isset($fileinfo['itemid'])) { $itemid = $fileinfo['itemid']; } else { @@ -238,16 +255,21 @@ $context = get_system_context(); } - $fs = get_file_storage(); + +// TODO: we MUST obey access control restrictions here, no messing with file_storage here, the only allowed way is to use file_browser here!!!!!!!!!!!!!!!!!!!!!!!! +throw new coding_exception('File upload ext api needs to be made secure first!!!!'); + + $browser = get_file_browser(); // check existing file - if ($file = $fs->get_file($context->id, $filearea, $itemid, $filepath, $filename)) { + if ($file = $fs->get_file($context->id, $component, $filearea, $itemid, $filepath, $filename)) { throw new moodle_exception('fileexist'); } $file_record = new object(); $file_record->contextid = $context->id; + $file_record->component = $component; $file_record->filearea = $filearea; $file_record->itemid = $itemid; $file_record->filepath = $filepath; Index: files/index.php =================================================================== RCS file: /cvsroot/moodle/moodle/files/index.php,v retrieving revision 1.161 diff -u -r1.161 index.php --- files/index.php 22 Jun 2010 09:04:47 -0000 1.161 +++ files/index.php 28 Jun 2010 22:31:57 -0000 @@ -18,7 +18,7 @@ /** * Moodle file tree viewer based on YUI2 Treeview * - * @package moodlecore + * @package core * @subpackage file * @copyright 2010 Dongsheng Cai * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later @@ -26,35 +26,19 @@ require('../config.php'); -$courseid = optional_param('id', 0, PARAM_INT); - $contextid = optional_param('contextid', SYSCONTEXTID, PARAM_INT); +$component = optional_param('component', '', PARAM_ALPHAEXT); $filearea = optional_param('filearea', '', PARAM_ALPHAEXT); $itemid = optional_param('itemid', -1, PARAM_INT); $filepath = optional_param('filepath', '', PARAM_PATH); $filename = optional_param('filename', '', PARAM_FILE); -if ($courseid) { - $course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST); - $context = get_context_instance(CONTEXT_COURSE, $course->id, MUST_EXIST); - redirect(new moodle_url('index.php', array('contextid' => $context->id, 'itemid'=> 0, 'filearea' => 'course_content'))); -} - -$context = get_context_instance_by_id($contextid, MUST_EXIST); -$PAGE->set_context($context); +$PAGE->set_url('/files/index.php', array('contextid'=>$contextid, 'component'=>$component, 'filearea'=>$filearea, 'itemid'=>$itemid, 'filepath'=>$filepath, 'filename'=>$filename)); -$course = null; -$cm = null; -if ($context->contextlevel == CONTEXT_MODULE) { - $cm = get_coursemodule_from_id(null, $context->instanceid, 0, false, MUST_EXIST); - $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST); -} else if ($context->contextlevel == CONTEXT_COURSE) { - $course = $DB->get_record('course', array('id'=>$context->instanceid), '*', MUST_EXIST); +if ($component === '') { + $component = null; } -require_login($course, false, $cm); -require_capability('moodle/course:managefiles', $context); - if ($filearea === '') { $filearea = null; } @@ -71,11 +55,21 @@ $filename = null; } +list($context, $course, $cm) = get_context_info_array($contextid); + +require_login($course, false, $cm); +require_capability('moodle/course:managefiles', $context); + $browser = get_file_browser(); -$file_info = $browser->get_file_info($context, $filearea, $itemid, $filepath, $filename); +$file_info = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename); $strfiles = get_string("files"); + +$PAGE->navbar->add($strfiles); +$PAGE->set_title("$SITE->shortname: $strfiles"); +$PAGE->set_heading($SITE->fullname); + if ($context->contextlevel == CONTEXT_MODULE) { $PAGE->set_pagelayout('incourse'); } else if ($context->contextlevel == CONTEXT_COURSE) { @@ -84,16 +78,20 @@ $PAGE->set_pagelayout('admin'); } -$PAGE->navbar->add($strfiles); -$PAGE->set_url("/files/index.php", $file_info->get_params()); -$PAGE->set_title("$SITE->shortname: $strfiles"); -$PAGE->set_heading($SITE->fullname); -echo $OUTPUT->header(); +$output = $PAGE->get_renderer('core', 'files'); + +echo $output->header(); +echo $output->box_start(); + +if ($file_info) { + $options = array(); + //$options['visible_areas'] = array('backup'=>array('section', 'course'), 'course'=>array('legacy'), 'user'=>array('backup')); + echo $output->files_tree_viewer($file_info, $options); +} else { + notify(get_string('nofilesavailable', 'repository')); +} + +echo $output->box_end(); -$options = array(); -$options['enabled_fileareas'] = array('section_backup', 'course_backup', 'course_content', 'user_backup'); -echo $OUTPUT->box_start(); -echo $OUTPUT->moodle_file_tree_viewer($context->id, $filearea, $itemid, $filepath, $options); -echo $OUTPUT->box_end(); +echo $output->footer(); -echo $OUTPUT->footer(); Index: blog/edit.php =================================================================== RCS file: /cvsroot/moodle/moodle/blog/edit.php,v retrieving revision 1.86 diff -u -r1.86 edit.php --- blog/edit.php 22 Apr 2010 01:58:01 -0000 1.86 +++ blog/edit.php 28 Jun 2010 22:31:50 -0000 @@ -94,7 +94,7 @@ $userid = $entry->userid; $entry->subject = clean_text($entry->subject); $entry->summary = clean_text($entry->summary, $entry->format); - + } else { if (!has_capability('moodle/blog:create', $sitecontext)) { print_error('noentry', 'blog'); // manageentries is not enough for adding @@ -163,8 +163,8 @@ $blogeditform = new blog_edit_form(null, compact('entry', 'summaryoptions', 'attachmentoptions', 'sitecontext', 'courseid', 'modid')); -$entry = file_prepare_standard_editor($entry, 'summary', $summaryoptions, $sitecontext, 'blog_post', $entry->id); -$entry = file_prepare_standard_filemanager($entry, 'attachment', $attachmentoptions, $sitecontext, 'blog_attachment', $entry->id); +$entry = file_prepare_standard_editor($entry, 'summary', $summaryoptions, $sitecontext, 'blog', 'post', $entry->id); +$entry = file_prepare_standard_filemanager($entry, 'attachment', $attachmentoptions, $sitecontext, 'blog', 'attachment', $entry->id); if (!empty($CFG->usetags) && !empty($entry->id)) { include_once($CFG->dirroot.'/tag/lib.php'); Index: blog/locallib.php =================================================================== RCS file: /cvsroot/moodle/moodle/blog/locallib.php,v retrieving revision 1.27 diff -u -r1.27 locallib.php --- blog/locallib.php 21 Jun 2010 08:57:59 -0000 1.27 +++ blog/locallib.php 28 Jun 2010 22:31:50 -0000 @@ -110,7 +110,7 @@ $cmt->showcount = $CFG->blogshowcommentscount; $options->comments = $cmt; } - $this->summary = file_rewrite_pluginfile_urls($this->summary, 'pluginfile.php', SYSCONTEXTID, 'blog_post', $this->id); + $this->summary = file_rewrite_pluginfile_urls($this->summary, 'pluginfile.php', SYSCONTEXTID, 'blog', 'post', $this->id); $template['body'] = format_text($this->summary, $this->summaryformat, $options); $template['title'] = ''; @@ -368,8 +368,8 @@ $entry->$var = $val; } - $entry = file_postupdate_standard_editor($entry, 'summary', $summaryoptions, $sitecontext, 'blog_post', $entry->id); - $entry = file_postupdate_standard_filemanager($entry, 'attachment', $attachmentoptions, $sitecontext, 'blog_attachment', $entry->id); + $entry = file_postupdate_standard_editor($entry, 'summary', $summaryoptions, $sitecontext, 'blog', 'post', $entry->id); + $entry = file_postupdate_standard_filemanager($entry, 'attachment', $attachmentoptions, $sitecontext, 'blog', 'attachment', $entry->id); if (!empty($CFG->useblogassociations)) { $entry->add_associations(); @@ -461,8 +461,8 @@ */ public function delete_attachments() { $fs = get_file_storage(); - $fs->delete_area_files(SYSCONTEXTID, 'blog_attachment', $this->id); - $fs->delete_area_files(SYSCONTEXTID, 'blog_post', $this->id); + $fs->delete_area_files(SYSCONTEXTID, 'blog', 'attachment', $this->id); + $fs->delete_area_files(SYSCONTEXTID, 'blog', 'post', $this->id); } /** @@ -479,9 +479,8 @@ require_once($CFG->libdir.'/filelib.php'); $fs = get_file_storage(); - $browser = get_file_browser(); - $files = $fs->get_area_files(SYSCONTEXTID, 'blog_attachment', $this->id); + $files = $fs->get_area_files(SYSCONTEXTID, 'blog', 'attachment', $this->id); $imagereturn = ""; $output = ""; @@ -494,7 +493,7 @@ } $filename = $file->get_filename(); - $ffurl = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.SYSCONTEXTID.'/blog_attachment/'.$this->id.'/'.$filename); + $ffurl = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.SYSCONTEXTID.'/blog/attachment/'.$this->id.'/'.$filename); $mimetype = $file->get_mimetype(); $icon = substr(mimeinfo_from_type("icon", $mimetype), 0, -4); Index: mod/chat/backup/moodle2/backup_chat_stepslib.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/chat/backup/moodle2/backup_chat_stepslib.php,v retrieving revision 1.1 diff -u -r1.1 backup_chat_stepslib.php --- mod/chat/backup/moodle2/backup_chat_stepslib.php 6 May 2010 09:30:42 -0000 1.1 +++ mod/chat/backup/moodle2/backup_chat_stepslib.php 28 Jun 2010 22:32:36 -0000 @@ -57,7 +57,7 @@ $message->annotate_ids('group', 'groupid'); // Annotate the file areas in chat module - $chat->annotate_files(array('chat_intro'), null); // chat_intro area don't use itemid + $chat->annotate_files('mod_chat', 'intro', null); // chat_intro area don't use itemid // Return the root element (chat), wrapped into standard activity structure return $this->prepare_activity_structure($chat); Index: mod/lesson/pagetypes/endofcluster.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/lesson/pagetypes/endofcluster.php,v retrieving revision 1.3 diff -u -r1.3 endofcluster.php --- mod/lesson/pagetypes/endofcluster.php 11 Feb 2010 16:56:01 -0000 1.3 +++ mod/lesson/pagetypes/endofcluster.php 28 Jun 2010 22:32:54 -0000 @@ -73,7 +73,7 @@ if (empty($properties->qoption)) { $properties->qoption = '0'; } - $properties = file_postupdate_standard_editor($properties, 'contents', array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$PAGE->course->maxbytes), get_context_instance(CONTEXT_MODULE, $PAGE->cm->id), 'lesson_page_contents', $properties->id); + $properties = file_postupdate_standard_editor($properties, 'contents', array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$PAGE->course->maxbytes), get_context_instance(CONTEXT_MODULE, $PAGE->cm->id), 'mod_lesson', 'page_contents', $properties->id); $DB->update_record("lesson_pages", $properties); $answers = $this->get_answers(); Index: mod/lesson/pagetypes/matching.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/lesson/pagetypes/matching.php,v retrieving revision 1.4 diff -u -r1.4 matching.php --- mod/lesson/pagetypes/matching.php 17 Feb 2010 19:33:25 -0000 1.4 +++ mod/lesson/pagetypes/matching.php 28 Jun 2010 22:32:55 -0000 @@ -299,7 +299,7 @@ $answers = $this->get_answers(); $properties->id = $this->properties->id; $properties->lessonid = $this->lesson->id; - $properties = file_postupdate_standard_editor($properties, 'contents', array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$PAGE->course->maxbytes), get_context_instance(CONTEXT_MODULE, $PAGE->cm->id), 'lesson_page_contents', $properties->id); + $properties = file_postupdate_standard_editor($properties, 'contents', array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$PAGE->course->maxbytes), get_context_instance(CONTEXT_MODULE, $PAGE->cm->id), 'mod_lesson', 'page_contents', $properties->id); $DB->update_record("lesson_pages", $properties); // need to add two to offset correct response and wrong response Index: mod/lesson/pagetypes/essay.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/lesson/pagetypes/essay.php,v retrieving revision 1.3 diff -u -r1.3 essay.php --- mod/lesson/pagetypes/essay.php 16 Jan 2010 15:40:03 -0000 1.3 +++ mod/lesson/pagetypes/essay.php 28 Jun 2010 22:32:55 -0000 @@ -120,7 +120,7 @@ $answers = $this->get_answers(); $properties->id = $this->properties->id; $properties->lessonid = $this->lesson->id; - $properties = file_postupdate_standard_editor($properties, 'contents', array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$PAGE->course->maxbytes), get_context_instance(CONTEXT_MODULE, $PAGE->cm->id), 'lesson_page_contents', $properties->id); + $properties = file_postupdate_standard_editor($properties, 'contents', array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$PAGE->course->maxbytes), get_context_instance(CONTEXT_MODULE, $PAGE->cm->id), 'mod_lesson', 'page_contents', $properties->id); $DB->update_record("lesson_pages", $properties); if (!array_key_exists(0, $this->answers)) { Index: mod/lesson/pagetypes/endofbranch.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/lesson/pagetypes/endofbranch.php,v retrieving revision 1.5 diff -u -r1.5 endofbranch.php --- mod/lesson/pagetypes/endofbranch.php 4 Mar 2010 05:25:00 -0000 1.5 +++ mod/lesson/pagetypes/endofbranch.php 28 Jun 2010 22:32:54 -0000 @@ -104,7 +104,7 @@ if (empty($properties->qoption)) { $properties->qoption = '0'; } - $properties = file_postupdate_standard_editor($properties, 'contents', array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$PAGE->course->maxbytes), get_context_instance(CONTEXT_MODULE, $PAGE->cm->id), 'lesson_page_contents', $properties->id); + $properties = file_postupdate_standard_editor($properties, 'contents', array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$PAGE->course->maxbytes), get_context_instance(CONTEXT_MODULE, $PAGE->cm->id), 'mod_lesson', 'page_contents', $properties->id); $DB->update_record("lesson_pages", $properties); $answers = $this->get_answers(); Index: admin/settings/grades.php =================================================================== RCS file: /cvsroot/moodle/moodle/admin/settings/grades.php,v retrieving revision 1.54 diff -u -r1.54 grades.php --- admin/settings/grades.php 25 Jun 2010 17:21:15 -0000 1.54 +++ admin/settings/grades.php 28 Jun 2010 22:31:44 -0000 @@ -26,8 +26,7 @@ // new CFG variable for gradebook (what roles to display) $temp->add(new admin_setting_special_gradebookroles()); - // enable outcomes checkbox - $temp->add(new admin_setting_configcheckbox('enableoutcomes', get_string('enableoutcomes', 'grades'), get_string('enableoutcomes_help', 'grades'), 0)); + // enable outcomes checkbox now in subsystems area $temp->add(new admin_setting_grade_profilereport()); Index: mod/imscp/mod_form.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/imscp/mod_form.php,v retrieving revision 1.4 diff -u -r1.4 mod_form.php --- mod/imscp/mod_form.php 22 May 2010 13:54:41 -0000 1.4 +++ mod/imscp/mod_form.php 28 Jun 2010 22:32:51 -0000 @@ -74,7 +74,7 @@ $usercontext = get_context_instance(CONTEXT_USER, $USER->id); $fs = get_file_storage(); - if (!$files = $fs->get_area_files($usercontext->id, 'user_draft', $data['package'], 'id', false)) { + if (!$files = $fs->get_area_files($usercontext->id, 'user', 'draft', $data['package'], 'id', false)) { if (!$this->current->instance) { $errors['package'] = get_string('required'); return $errors; @@ -86,7 +86,7 @@ if ($file->get_mimetype() != 'application/zip') { $errors['package'] = get_string('invalidfiletype', 'error', '', $file); // better delete current file, it is not usable anyway - $fs->delete_area_files($usercontext->id, 'user_draft', $data['package']); + $fs->delete_area_files($usercontext->id, 'user', 'draft', $data['package']); } return $errors; Index: mod/imscp/locallib.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/imscp/locallib.php,v retrieving revision 1.3 diff -u -r1.3 locallib.php --- mod/imscp/locallib.php 6 Feb 2010 19:44:58 -0000 1.3 +++ mod/imscp/locallib.php 28 Jun 2010 22:32:51 -0000 @@ -24,7 +24,6 @@ */ require_once("$CFG->dirroot/mod/imscp/lib.php"); -require_once("$CFG->libdir/file/file_browser.php"); require_once("$CFG->libdir/filelib.php"); require_once("$CFG->libdir/resourcelib.php"); @@ -35,7 +34,7 @@ $first = reset($items); $context = get_context_instance(CONTEXT_MODULE, $cm->id); $urlbase = "$CFG->wwwroot/pluginfile.php"; - $path = '/'.$context->id.'/imscp_content/'.$imscp->revision.'/'.$first['href']; + $path = '/'.$context->id.'/mod_imscp/content/'.$imscp->revision.'/'.$first['href']; $firsturl = file_encode_url($urlbase, $path, false); echo '
'; @@ -60,7 +59,7 @@ $context = get_context_instance(CONTEXT_MODULE, $cm->id); $urlbase = "$CFG->wwwroot/pluginfile.php"; - $path = '/'.$context->id.'/imscp_content/'.$imscp->revision.'/'.$item['href']; + $path = '/'.$context->id.'/mod_imscp/content/'.$imscp->revision.'/'.$item['href']; $url = file_encode_url($urlbase, $path, false); $result = "
  • ".$item['title'].''; if ($item['subitems']) { @@ -78,7 +77,7 @@ function imscp_parse_structure($imscp, $context) { $fs = get_file_storage(); - if (!$manifestfile = $fs->get_file($context->id, 'imscp_content', $imscp->revision, '/', 'imsmanifest.xml')) { + if (!$manifestfile = $fs->get_file($context->id, 'mod_imscp', 'content', $imscp->revision, '/', 'imsmanifest.xml')) { return null; } @@ -212,6 +211,7 @@ */ public function get_params() { return array('contextid'=>$this->context->id, + 'component'=>'mod_imscp', 'filearea' =>$this->filearea, 'itemid' =>null, 'filepath' =>null, @@ -250,9 +250,9 @@ global $DB; $children = array(); - $itemids = $DB->get_records('files', array('contextid'=>$this->context->id, 'filearea'=>$this->filearea), 'itemid', "DISTINCT itemid"); + $itemids = $DB->get_records('files', array('contextid'=>$this->context->id, 'component'=>'mod_imscp', 'filearea'=>$this->filearea), 'itemid', "DISTINCT itemid"); foreach ($itemids as $itemid=>$unused) { - if ($child = $this->browser->get_file_info($this->context, $this->filearea, $itemid)) { + if ($child = $this->browser->get_file_info($this->context, 'mod_imscp', $this->filearea, $itemid)) { $children[] = $child; } } Index: mod/imscp/lib.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/imscp/lib.php,v retrieving revision 1.6 diff -u -r1.6 lib.php --- mod/imscp/lib.php 4 May 2010 18:28:31 -0000 1.6 +++ mod/imscp/lib.php 28 Jun 2010 22:32:51 -0000 @@ -101,10 +101,10 @@ $imscp = $DB->get_record('imscp', array('id'=>$data->id), '*', MUST_EXIST); if ($filename = $mform->get_new_filename('package')) { - if ($package = $mform->save_stored_file('package', $context->id, 'imscp_backup', 1, '/', $filename)) { + if ($package = $mform->save_stored_file('package', $context->id, 'mod_imscp', 'backup', 1, '/', $filename)) { // extract package content $packer = get_file_packer('application/zip'); - $package->extract_to_storage($packer, $context->id, 'imscp_content', 1, '/'); + $package->extract_to_storage($packer, $context->id, 'mod_imscp', 'content', 1, '/'); $structure = imscp_parse_structure($imscp, $context); $imscp->structure = is_array($structure) ? serialize($structure) : null; $DB->update_record('imscp', $imscp); @@ -143,26 +143,26 @@ // get a list of existing packages before adding new package if ($imscp->keepold > -1) { - $packages = $fs->get_area_files($context->id, 'imscp_backup', false, "itemid ASC", false); + $packages = $fs->get_area_files($context->id, 'mod_imscp', 'backup', false, "itemid ASC", false); } else { $packages = array(); } - $package = $mform->save_stored_file('package', $context->id, 'imscp_backup', $imscp->revision, '/', $filename); + $package = $mform->save_stored_file('package', $context->id, 'mod_imscp', 'backup', $imscp->revision, '/', $filename); // purge all extracted content - $fs->delete_area_files($context->id, 'imscp_content'); + $fs->delete_area_files($context->id, 'mod_imscp', 'content'); // extract package content if ($package) { $packer = get_file_packer('application/zip'); - $package->extract_to_storage($packer, $context->id, 'imscp_content', $imscp->revision, '/'); + $package->extract_to_storage($packer, $context->id, 'mod_imscp', 'content', $imscp->revision, '/'); } // cleanup old package files, keep current + keepold while ($packages and (count($packages) > $imscp->keepold)) { $package = array_shift($packages); - $fs->delete_area_files($context->id, 'imscp_backup', $package->get_itemid()); + $fs->delete_area_files($context->id, 'mod_imscp', 'backup', $package->get_itemid()); } } @@ -262,10 +262,10 @@ */ function imscp_get_file_areas($course, $cm, $context) { $areas = array(); - if (has_capability('moodle/course:managefiles', $context)) { - $areas['imscp_content'] = get_string('areacontent', 'imscp'); - $areas['imscp_backup'] = get_string('areabackup', 'imscp'); - } + + $areas['content'] = get_string('areacontent', 'imscp'); + $areas['backup'] = get_string('areabackup', 'imscp'); + return $areas; } @@ -284,18 +284,20 @@ */ function imscp_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) { global $CFG, $DB; - require_once("$CFG->dirroot/mod/imscp/locallib.php"); // note: imscp_intro handled in file_browser automatically if (!has_capability('moodle/course:managefiles', $context)) { - // no peaking here, sorry + // no peaking here for students!! return null; } - if ($filearea !== 'imscp_content' and $filearea !== 'imscp_backup') { + if ($filearea !== 'content' and $filearea !== 'backup') { return null; } + + require_once("$CFG->dirroot/mod/imscp/locallib.php"); + if (is_null($itemid)) { return new imscp_file_info($browser, $course, $cm, $context, $areas, $filearea, $itemid); } @@ -303,49 +305,46 @@ $fs = get_file_storage(); $filepath = is_null($filepath) ? '/' : $filepath; $filename = is_null($filename) ? '.' : $filename; - if (!$storedfile = $fs->get_file($context->id, $filearea, $itemid, $filepath, $filename)) { + if (!$storedfile = $fs->get_file($context->id, 'mod_imscp', $filearea, $itemid, $filepath, $filename)) { return null; } // do not allow manual modification of any files! $urlbase = $CFG->wwwroot.'/pluginfile.php'; - return new file_info_stored($browser, $context, $storedfile, $urlbase, $itemid, true, true, false, false); + return new file_info_stored($browser, $context, $storedfile, $urlbase, $itemid, true, true, false, false); //no writing here! } /** * Serves the imscp files. * * @param object $course - * @param object $cminfo + * @param object $cm * @param object $context * @param string $filearea * @param array $args * @param bool $forcedownload * @return bool false if file not found, does not return if found - justsend the file */ -function imscp_pluginfile($course, $cminfo, $context, $filearea, $args, $forcedownload) { +function imscp_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload) { global $CFG, $DB; - if (!$cminfo->uservisible) { + if ($context->contextlevel != CONTEXT_MODULE) { return false; } - if (!$cm = get_coursemodule_from_instance('imscp', $cminfo->instance, $course->id)) { - return false; - } + require_login($course, true, $cm); - if ($filearea === 'imscp_content') { - require_course_login($course, true, $cm); + if ($filearea === 'content') { $revision = array_shift($args); $fs = get_file_storage(); - $relativepath = '/'.implode('/', $args); - if ($relativepath === '/imsmanifest.xml') { + $relativepath = implode('/', $args); + if ($relativepath === 'imsmanifest.xml') { if (!has_capability('moodle/course:managefiles', $context)) { // no stealing of detailed package info ;-) return false; } } - $fullpath = $context->id.$filearea.$revision.$relativepath; + $fullpath = "/$context->id/mod_imscp/$filearea/$revision/$relativepath"; if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) { return false; } @@ -353,16 +352,15 @@ // finally send the file send_stored_file($file, 86400, 0, $forcedownload); - } else if ($filearea === 'imscp_backup') { - require_login($course, false, $cm); + } else if ($filearea === 'backup') { if (!has_capability('moodle/course:managefiles', $context)) { // no stealing of package backups return false; } $revision = array_shift($args); $fs = get_file_storage(); - $relativepath = '/'.implode('/', $args); - $fullpath = $context->id.$filearea.$revision.$relativepath; + $relativepath = implode('/', $args); + $fullpath = "/$context->id/mod_imscp/$filearea/$revision/$relativepath"; if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) { return false; } Index: repository/local/repository.class.php =================================================================== RCS file: /cvsroot/moodle/moodle/repository/local/repository.class.php,v retrieving revision 1.57 diff -u -r1.57 repository.class.php --- repository/local/repository.class.php 22 Jun 2010 03:55:45 -0000 1.57 +++ repository/local/repository.class.php 28 Jun 2010 22:33:11 -0000 @@ -75,6 +75,7 @@ $itemid = $params['itemid']; $filename = $params['filename']; $filearea = $params['filearea']; + $component = $params['component']; $filepath = $params['filepath']; $context = get_context_instance_by_id($params['contextid']); } @@ -82,6 +83,7 @@ $itemid = null; $filename = null; $filearea = null; + $component = null; $filepath = null; $context = get_system_context(); } @@ -89,7 +91,7 @@ try { $browser = get_file_browser(); - if ($fileinfo = $browser->get_file_info($context, $filearea, $itemid, $filepath, $filename)) { + if ($fileinfo = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename)) { // build path navigation $pathnodes = array(); $encodedpath = base64_encode(serialize($fileinfo->get_params())); @@ -117,7 +119,7 @@ $encodedpath = base64_encode(serialize($params)); // hide user_private area from local plugin, user should // use private file plugin to access private files - if ($params['filearea'] == 'user_private') { + if ($params['component'] = 'user' and $params['filearea'] == 'private') { continue; } $node = array( @@ -182,7 +184,7 @@ * @param string $new_filepath the new path in draft area * @return array The information of file */ - public function copy_to_area($encoded, $new_filearea='user_draft', $new_itemid = '', $new_filepath = '/', $new_filename = '') { + public function copy_to_area($encoded, $new_filearea='ignored', $new_itemid = '', $new_filepath = '/', $new_filename = '') { global $USER, $DB; $info = array(); @@ -191,14 +193,15 @@ $user_context = get_context_instance(CONTEXT_USER, $USER->id); // the final file $contextid = $params['contextid']; + $component = $params['component']; $filearea = $params['filearea']; $filepath = $params['filepath']; $filename = $params['filename']; $fileitemid = $params['itemid']; $context = get_context_instance_by_id($contextid); try { - $file_info = $browser->get_file_info($context, $filearea, $fileitemid, $filepath, $filename); - $file_info->copy_to_storage($user_context->id, $new_filearea, $new_itemid, $new_filepath, $new_filename); + $file_info = $browser->get_file_info($context, $component, $filearea, $fileitemid, $filepath, $filename); + $file_info->copy_to_storage($user_context->id, 'user', 'draft', $new_itemid, $new_filepath, $new_filename); } catch (Exception $e) { throw $e; } Index: lib/grade/grade_outcome.php =================================================================== RCS file: /cvsroot/moodle/moodle/lib/grade/grade_outcome.php,v retrieving revision 1.35 diff -u -r1.35 grade_outcome.php --- lib/grade/grade_outcome.php 1 Dec 2009 14:47:14 -0000 1.35 +++ lib/grade/grade_outcome.php 28 Jun 2010 22:32:30 -0000 @@ -98,7 +98,7 @@ if (parent::delete($source)) { $context = get_context_instance(CONTEXT_SYSTEM); $fs = get_file_storage(); - $files = $fs->get_area_files($context->id, 'grade_outcome', $this->id); + $files = $fs->get_area_files($context->id, 'grade', 'outcome', $this->id); foreach ($files as $file) { $file->delete(); } @@ -277,7 +277,7 @@ $options = new stdClass; $options->noclean = true; $systemcontext = get_context_instance(CONTEXT_SYSTEM); - $description = file_rewrite_pluginfile_urls($this->description, 'pluginfile.php', $systemcontext->id, 'grade_outcome', $this->id); + $description = file_rewrite_pluginfile_urls($this->description, 'pluginfile.php', $systemcontext->id, 'grade', 'outcome', $this->id); return format_text($description, $this->descriptionformat, $options); } Index: lib/grade/grade_scale.php =================================================================== RCS file: /cvsroot/moodle/moodle/lib/grade/grade_scale.php,v retrieving revision 1.29 diff -u -r1.29 grade_scale.php --- lib/grade/grade_scale.php 21 May 2010 18:57:05 -0000 1.29 +++ lib/grade/grade_scale.php 28 Jun 2010 22:32:30 -0000 @@ -129,7 +129,7 @@ if (parent::delete($source)) { $context = get_context_instance(CONTEXT_SYSTEM); $fs = get_file_storage(); - $files = $fs->get_area_files($context->id, 'grade_scale', $this->id); + $files = $fs->get_area_files($context->id, 'grade', 'scale', $this->id); foreach ($files as $file) { $file->delete(); } @@ -314,7 +314,7 @@ $systemcontext = get_context_instance(CONTEXT_SYSTEM); $options = new stdClass; $options->noclean = true; - $description = file_rewrite_pluginfile_urls($this->description, 'pluginfile.php', $systemcontext->id, 'grade_scale', $this->id); + $description = file_rewrite_pluginfile_urls($this->description, 'pluginfile.php', $systemcontext->id, 'grade', 'scale', $this->id); return format_text($description, $this->descriptionformat, $options); } } Index: mod/forum/locallib.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/forum/locallib.php,v retrieving revision 1.8 diff -u -r1.8 locallib.php --- mod/forum/locallib.php 19 May 2010 17:42:47 -0000 1.8 +++ mod/forum/locallib.php 28 Jun 2010 22:32:46 -0000 @@ -97,8 +97,8 @@ if ($this->attachment) { $this->set_file_and_format_data($this->attachment); } else { - $attach = $fs->get_area_files($this->modcontext->id, 'forum_attachment', $this->post->id, 'timemodified', false); - $embed = $fs->get_area_files($this->modcontext->id, 'forum_post', $this->post->id, 'timemodified', false); + $attach = $fs->get_area_files($this->modcontext->id, 'mod_forum', 'attachment', $this->post->id, 'timemodified', false); + $embed = $fs->get_area_files($this->modcontext->id, 'mod_forum', 'post', $this->post->id, 'timemodified', false); $files = array_merge($attach, $embed); $this->set_file_and_format_data($files); } @@ -112,8 +112,8 @@ $this->posts = forum_get_all_discussion_posts($this->discussion->id, 'p.created ASC'); $this->multifiles = array(); foreach ($this->posts as $post) { - $attach = $fs->get_area_files($this->modcontext->id, 'forum_attachment', $post->id, 'timemodified', false); - $embed = $fs->get_area_files($this->modcontext->id, 'forum_post', $post->id, 'timemodified', false); + $attach = $fs->get_area_files($this->modcontext->id, 'mod_forum', 'attachment', $post->id, 'timemodified', false); + $embed = $fs->get_area_files($this->modcontext->id, 'mod_forum', 'post', $post->id, 'timemodified', false); $files = array_merge($attach, $embed); if ($files) { $this->keyedfiles[$post->id] = $files; @@ -304,7 +304,7 @@ $options->para = true; $format = $this->get('exporter')->get('format'); $formattedtext = format_text($post->message, $post->messageformat, $options, $this->get('course')->id); - $formattedtext = portfolio_rewrite_pluginfile_urls($formattedtext, $this->modcontext->id, 'forum_post', $post->id, $format); + $formattedtext = portfolio_rewrite_pluginfile_urls($formattedtext, $this->modcontext->id, 'mod_forum', 'post', $post->id, $format); $output = ''; Index: mod/forum/post.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/forum/post.php,v retrieving revision 1.212 diff -u -r1.212 post.php --- mod/forum/post.php 21 Jun 2010 15:30:59 -0000 1.212 +++ mod/forum/post.php 28 Jun 2010 22:32:47 -0000 @@ -51,7 +51,7 @@ $sitecontext = get_context_instance(CONTEXT_SYSTEM); if (!isloggedin() or isguestuser()) { - + if (!isloggedin() and !get_referer()) { // No referer+not logged in - probably coming in via email See MDL-9052 require_login(); @@ -270,7 +270,7 @@ $post = trusttext_pre_edit($post, 'message', $modcontext); unset($SESSION->fromdiscussion); - + }else if (!empty($delete)) { // User is deleting a post @@ -297,8 +297,8 @@ || has_capability('mod/forum:deleteanypost', $modcontext)) ) { print_error('cannotdeletepost', 'forum'); } - - + + $replycount = forum_count_replies($post); if (!empty($confirm) && confirm_sesskey()) { // User has confirmed the delete @@ -493,7 +493,7 @@ $mform_post = new mod_forum_post_form('post.php', array('course'=>$course, 'cm'=>$cm, 'coursecontext'=>$coursecontext, 'modcontext'=>$modcontext, 'forum'=>$forum, 'post'=>$post)); $draftitemid = file_get_submitted_draft_itemid('attachments'); -file_prepare_draft_area($draftitemid, $modcontext->id, 'forum_attachment', empty($post->id)?null:$post->id); +file_prepare_draft_area($draftitemid, $modcontext->id, 'mod_forum', 'attachment', empty($post->id)?null:$post->id); //load data into form NOW! @@ -531,7 +531,7 @@ } $draftid_editor = file_get_submitted_draft_itemid('message'); -$currenttext = file_prepare_draft_area($draftid_editor, $modcontext->id, 'forum_post', empty($post->id) ? null : $post->id, array('subdirs'=>true), $post->message); +$currenttext = file_prepare_draft_area($draftid_editor, $modcontext->id, 'mod_forum', 'post', empty($post->id) ? null : $post->id, array('subdirs'=>true), $post->message); $mform_post->set_data(array( 'attachments'=>$draftitemid, 'general'=>$heading, 'subject'=>$post->subject, Index: mod/forum/lib.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/forum/lib.php,v retrieving revision 1.874 diff -u -r1.874 lib.php --- mod/forum/lib.php 28 Jun 2010 17:04:36 -0000 1.874 +++ mod/forum/lib.php 28 Jun 2010 22:32:46 -0000 @@ -3103,7 +3103,7 @@ $post->course = $course->id; $post->forum = $forum->id; - $post->message = file_rewrite_pluginfile_urls($post->message, 'pluginfile.php', $modcontext->id, 'forum_post', $post->id); + $post->message = file_rewrite_pluginfile_urls($post->message, 'pluginfile.php', $modcontext->id, 'mod_forum', 'post', $post->id); // caching if (!isset($cm->cache)) { @@ -3714,13 +3714,13 @@ // loop through all posts, better not use attachment flag ;-) if ($posts = $DB->get_records('forum_posts', array('discussion'=>$discussion->id), '', 'id, attachment')) { foreach ($posts as $post) { - if ($oldfiles = $fs->get_area_files($oldcontext->id, 'forum_attachment', $post->id, "id", false)) { + if ($oldfiles = $fs->get_area_files($oldcontext->id, 'mod_forum', 'attachment', $post->id, "id", false)) { foreach ($oldfiles as $oldfile) { $file_record = new object(); $file_record->contextid = $newcontext->id; $fs->create_file_from_storedfile($file_record, $oldfile); } - $fs->delete_area_files($oldcontext->id, 'forum_attachment', $post->id); + $fs->delete_area_files($oldcontext->id, 'mod_forum', 'attachment', $post->id); if ($post->attachment != '1') { //weird - let's fix it $post->attachment = '1'; @@ -3767,7 +3767,6 @@ $strattachment = get_string('attachment', 'forum'); $fs = get_file_storage(); - $browser = get_file_browser(); $imagereturn = ''; $output = ''; @@ -3775,13 +3774,13 @@ $canexport = (has_capability('mod/forum:exportpost', $context) || ($post->userid == $USER->id && has_capability('mod/forum:exportownpost', $context))); require_once($CFG->libdir.'/portfoliolib.php'); - if ($files = $fs->get_area_files($context->id, 'forum_attachment', $post->id, "timemodified", false)) { + if ($files = $fs->get_area_files($context->id, 'mod_forum', 'attachment', $post->id, "timemodified", false)) { $button = new portfolio_add_button(); foreach ($files as $file) { $filename = $file->get_filename(); $mimetype = $file->get_mimetype(); $iconimage = ''.$mimetype.''; - $path = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$context->id.'/forum_attachment/'.$post->id.'/'.$filename); + $path = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$context->id.'/mod_forum/attachment/'.$post->id.'/'.$filename); if ($type == 'html') { $output .= "$iconimage "; @@ -3844,33 +3843,29 @@ * Serves the forum attachments. Implements needed access control ;-) * * @param object $course - * @param object $cminfo + * @param object $cm * @param object $context * @param string $filearea * @param array $args * @param bool $forcedownload * @return bool false if file not found, does not return if found - justsend the file */ -function forum_pluginfile($course, $cminfo, $context, $filearea, $args, $forcedownload) { +function forum_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload) { global $CFG, $DB; - if (!$cminfo->uservisible) { + if ($context->contextlevel != CONTEXT_MODULE) { return false; } - $fileareas = array('forum_attachment', 'forum_post'); + require_course_login($course, true, $cm); + + $fileareas = array('attachment', 'post'); if (!in_array($filearea, $fileareas)) { return false; } $postid = (int)array_shift($args); - if (!$cm = get_coursemodule_from_instance('forum', $cminfo->instance, $course->id)) { - return false; - } - - require_course_login($course, true, $cm); - if (!$post = $DB->get_record('forum_posts', array('id'=>$postid))) { return false; } @@ -3879,19 +3874,19 @@ return false; } - if (!$forum = $DB->get_record('forum', array('id'=>$cminfo->instance))) { + if (!$forum = $DB->get_record('forum', array('id'=>$cm->instance))) { return false; } $fs = get_file_storage(); - $relativepath = '/'.implode('/', $args); - $fullpath = $context->id.$filearea.$postid.$relativepath; + $relativepath = implode('/', $args); + $fullpath = "/$context->id/mod_forum/$filearea/$postid/$relativepath"; if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) { return false; } // Make sure groups allow this user to see this file - if ($discussion->groupid > 0 and $groupmode = groups_get_activity_groupmode($cminfo, $course)) { // Groups are being used + if ($discussion->groupid > 0 and $groupmode = groups_get_activity_groupmode($cm, $course)) { // Groups are being used if (!groups_group_exists($discussion->groupid)) { // Can't find group return false; // Be safe and don't send it to anyone } @@ -3938,7 +3933,7 @@ $info = file_get_draft_area_info($post->attachments); $present = ($info['filecount']>0) ? '1' : ''; - file_save_draft_area_files($post->attachments, $context->id, 'forum_attachment', $post->id); + file_save_draft_area_files($post->attachments, $context->id, 'mod_forum', 'attachment', $post->id); $DB->set_field('forum_posts', 'attachment', $present, array('id'=>$post->id)); @@ -3971,7 +3966,7 @@ $post->attachment = ""; $post->id = $DB->insert_record("forum_posts", $post); - $message = file_save_draft_area_files($post->itemid, $context->id, 'forum_post', $post->id, array('subdirs'=>true), $message); + $message = file_save_draft_area_files($post->itemid, $context->id, 'mod_forum', 'post', $post->id, array('subdirs'=>true), $message); $DB->set_field('forum_posts', 'message', $message, array('id'=>$post->id)); forum_add_attachment($post, $forum, $cm, $mform, $message); @@ -4017,7 +4012,7 @@ $discussion->timestart = $post->timestart; $discussion->timeend = $post->timeend; } - $post->message = file_save_draft_area_files($post->itemid, $context->id, 'forum_post', $post->id, array('subdirs'=>true), $post->message); + $post->message = file_save_draft_area_files($post->itemid, $context->id, 'mod_forum', 'post', $post->id, array('subdirs'=>true), $post->message); $DB->set_field('forum_posts', 'message', $post->message, array('id'=>$post->id)); $DB->update_record('forum_discussions', $discussion); @@ -4080,7 +4075,7 @@ // TODO: Fix the calling code so that there always is a $cm when this function is called if (!empty($cm->id) && !empty($discussion->itemid)) { // In "single simple discussions" this may not exist yet $context = get_context_instance(CONTEXT_MODULE, $cm->id); - $text = file_save_draft_area_files($discussion->itemid, $context->id, 'forum_post', $post->id, array('subdirs'=>true), $post->message); + $text = file_save_draft_area_files($discussion->itemid, $context->id, 'mod_forum', 'post', $post->id, array('subdirs'=>true), $post->message); $DB->set_field('forum_posts', 'message', $text, array('id'=>$post->id)); } @@ -4195,7 +4190,7 @@ //delete attachments $fs = get_file_storage(); - $fs->delete_area_files($context->id, 'forum_attachment', $post->id); + $fs->delete_area_files($context->id, 'mod_forum', 'attachment', $post->id); if ($DB->delete_records("forum_posts", array("id" => $post->id))) { @@ -6957,7 +6952,7 @@ continue; } $context = get_context_instance(CONTEXT_MODULE, $cm->id); - $fs->delete_area_files($context->id, 'forum_attachment'); + $fs->delete_area_files($context->id, 'mod_forum', 'attachment'); //remove ratings $ratingdeloptions->contextid = $context->id; Index: mod/feedback/lib.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/feedback/lib.php,v retrieving revision 1.85 diff -u -r1.85 lib.php --- mod/feedback/lib.php 11 Jun 2010 09:12:57 -0000 1.85 +++ mod/feedback/lib.php 28 Jun 2010 22:32:40 -0000 @@ -138,40 +138,38 @@ * Serves the files included in feedback items like label. Implements needed access control ;-) * * @param object $course - * @param object $cminfo + * @param object $cm * @param object $context * @param string $filearea * @param array $args * @param bool $forcedownload * @return bool false if file not found, does not return if found - justsend the file */ -function feedback_pluginfile($course, $cminfo, $context, $filearea, $args, $forcedownload) { +function feedback_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload) { global $CFG, $DB; - if (!$cminfo->uservisible) { + if ($context->contextlevel != CONTEXT_MODULE) { return false; } - if($filearea === 'feedback_template') { + require_login($course, false, $cm); + + if ($filearea === 'template') { $usedcontext = get_context_instance(CONTEXT_COURSE, $course->id); }else { $usedcontext = $context; } - if ($filearea === 'feedback_item' OR $filearea === 'feedback_template') { + if ($filearea === 'item' OR $filearea === 'template') { $itemid = (int)array_shift($args); - if (!$cm = get_coursemodule_from_instance('feedback', $cminfo->instance, $course->id)) { - return false; - } - require_course_login($course, true, $cm); if (!$item = $DB->get_record('feedback_item', array('id'=>$itemid))) { return false; } - if (!$feedback = $DB->get_record('feedback', array('id'=>$cminfo->instance))) { + if (!$feedback = $DB->get_record('feedback', array('id'=>$cm->instance))) { return false; } @@ -179,14 +177,14 @@ return false; } - if ($item->feedback == $cminfo->instance) { + if ($item->feedback == $cm->instance) { $filecontext = $usedcontext; } else { return false; } - $relativepath = '/'.implode('/', $args); - $fullpath = $filecontext->id.$filearea.$itemid.$relativepath; + $relativepath = implode('/', $args); + $fullpath = "/$filecontext->id/mod_feedback/$filearea/$itemid/$relativepath"; $fs = get_file_storage(); if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) { @@ -918,7 +916,6 @@ global $DB; $fs = get_file_storage(); -die('TODO: MDL-21227 feedback code must not touch course summary files, code needs to be fixed, sorry'); if (!$feedbackitems = $DB->get_records('feedback_item', array('feedback'=>$feedback->id))) { return false; } @@ -947,11 +944,12 @@ $t_item->template = $newtempl->id; $t_item->id = $DB->insert_record('feedback_item', $t_item); //copy all included files to the feedback_template filearea - if ($itemfiles = $fs->get_area_files($f_context->id, 'feedback_item', $item->id, "id", false)) { + if ($itemfiles = $fs->get_area_files($f_context->id, 'mod_feedback', 'item', $item->id, "id", false)) { foreach($itemfiles as $ifile) { $file_record = new object(); $file_record->contextid = $c_context->id; - $file_record->filearea = 'course_summary'; + $file_record->component = 'mod_feedback'; + $file_record->filearea = 'template'; $file_record->itemid = $t_item->id; $fs->create_file_from_storedfile($file_record, $ifile); } @@ -985,7 +983,6 @@ function feedback_delete_template($id) { global $DB; -die('TODO: MDL-21227 feedback code must not touch course summary files, code needs to be fixed, sorry'); $template = $DB->get_record("feedback_template", array("id"=>$id)); //deleting the files from the item @@ -995,8 +992,8 @@ if($t_items = $DB->get_records("feedback_item", array("template"=>$id))) { foreach($t_items as $t_item) { - if ($templatefiles = $fs->get_area_files($context->id, 'course_summary', $t_item->id, "id", false)) { - $fs->delete_area_files($context->id, 'course_summary', $t_item->id); + if ($templatefiles = $fs->get_area_files($context->id, 'mod_feedback', 'template', $t_item->id, "id", false)) { + $fs->delete_area_files($context->id, 'mod_feedback', 'template', $t_item->id); } } } @@ -1019,8 +1016,6 @@ global $DB; $fs = get_file_storage(); -die('TODO: MDL-21227 feedback code must not touch course summary files, code needs to be fixed, sorry'); - //get all templateitems if(!$templitems = $DB->get_records('feedback_item', array('template'=>$templateid))) { return false; @@ -1067,11 +1062,12 @@ $item->id = $DB->insert_record('feedback_item', $item); //TODO: moving the files to the new items - if ($templatefiles = $fs->get_area_files($c_context->id, 'course_summary', $t_item->id, "id", false)) { + if ($templatefiles = $fs->get_area_files($c_context->id, 'mod_feedback', 'template', $t_item->id, "id", false)) { foreach($templatefiles as $tfile) { $file_record = new object(); $file_record->contextid = $f_context->id; - $file_record->filearea = 'feedback_item'; + $file_record->component = 'mod_feedback'; + $file_record->filearea = 'item'; $file_record->itemid = $item->id; $fs->create_file_from_storedfile($file_record, $tfile); } @@ -1295,8 +1291,8 @@ } $context = get_context_instance(CONTEXT_MODULE, $cm->id); - if ($itemfiles = $fs->get_area_files($context->id, 'feedback_item', $item->id, "id", false)) { - $fs->delete_area_files($context->id, 'feedback_item', $item->id); + if ($itemfiles = $fs->get_area_files($context->id, 'mod_feedback', 'item', $item->id, "id", false)) { + $fs->delete_area_files($context->id, 'mod_feedback', 'item', $item->id); } $DB->delete_records("feedback_value", array("item"=>$itemid)); Index: file.php =================================================================== RCS file: /cvsroot/moodle/moodle/file.php,v retrieving revision 1.61 diff -u -r1.61 file.php --- file.php 22 May 2010 13:10:05 -0000 1.61 +++ file.php 28 Jun 2010 22:31:43 -0000 @@ -62,7 +62,7 @@ } $courseid = (int)array_shift($args); -$relativepath = '/'.implode('/', $args); +$relativepath = implode('/', $args); // security: limit access to existing course subdirectories $course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST); @@ -77,8 +77,8 @@ } else if ($CFG->forcelogin) { if (!empty($CFG->sitepolicy) - and ($CFG->sitepolicy == $CFG->wwwroot.'/file.php'.$relativepath - or $CFG->sitepolicy == $CFG->wwwroot.'/file.php?file='.$relativepath)) { + and ($CFG->sitepolicy == $CFG->wwwroot.'/file.php/'.$relativepath + or $CFG->sitepolicy == $CFG->wwwroot.'/file.php?file=/'.$relativepath)) { //do not require login for policy file } else { require_login(0, true, null, false); @@ -89,7 +89,7 @@ $fs = get_file_storage(); -$fullpath = $context->id.'course_content0'.$relativepath; +$fullpath = "/$context->id/course/legacy/0/$relativepath"; if (!$file = $fs->get_file_by_hash(sha1($fullpath))) { if (strrpos($fullpath, '/') !== strlen($fullpath) -1 ) { Index: draftfile.php =================================================================== RCS file: /cvsroot/moodle/moodle/draftfile.php,v retrieving revision 1.12 diff -u -r1.12 draftfile.php --- draftfile.php 31 Oct 2009 22:02:06 -0000 1.12 +++ draftfile.php 28 Jun 2010 22:31:43 -0000 @@ -18,7 +18,7 @@ /** * This script serves draft files of current user * - * @package moodlecore + * @package core * @subpackage file * @copyright 2008 Petr Skoda (http://skodak.org) * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later @@ -52,11 +52,17 @@ } $contextid = (int)array_shift($args); -$filearea = array_shift($args); +$component = array_shift($args); +$filearea = array_shift($args); +$draftid = (int)array_shift($args); + +if ($component !== 'user' or $filearea !== 'draft') { + send_file_not_found(); +} $context = get_context_instance_by_id($contextid); if ($context->contextlevel != CONTEXT_USER) { - print_error('invalidarguments'); + send_file_not_found(); } $userid = $context->instanceid; @@ -64,20 +70,11 @@ print_error('invaliduserid'); } -switch ($filearea) { - case 'user_draft': - $itemid = (int)array_shift($args); - break; - default: - send_file_not_found(); -} - -$relativepath = '/'.implode('/', $args); - $fs = get_file_storage(); -$fullpath = $context->id.$filearea.$itemid.$relativepath; +$relativepath = implode('/', $args); +$fullpath = "/$context->id/user/draft/$draftid/$relativepath"; if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->get_filename() == '.') { send_file_not_found(); Index: userfile.php =================================================================== RCS file: userfile.php diff -N userfile.php --- userfile.php 3 May 2010 14:10:23 -0000 1.14 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,123 +0,0 @@ -. - -/** - * This script serves user's private files - * - * @package moodlecore - * @subpackage file - * @copyright 2008 Petr Skoda (http://skodak.org) - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -// disable moodle specific debug messages and any errors in output -define('NO_DEBUG_DISPLAY', true); - -require_once('config.php'); -require_once('lib/filelib.php'); - - -$relativepath = get_file_argument(); -$forcedownload = optional_param('forcedownload', 0, PARAM_BOOL); - -// relative path must start with '/' -if (!$relativepath) { - print_error('invalidargorconf'); -} else if ($relativepath{0} != '/') { - print_error('pathdoesnotstartslash'); -} - -// extract relative path components -$args = explode('/', ltrim($relativepath, '/')); - -if (count($args) == 0) { // always at least user id - print_error('invalidarguments'); -} - -$contextid = (int)array_shift($args); -$filearea = array_shift($args); - -$context = get_context_instance_by_id($contextid); -if ($context->contextlevel != CONTEXT_USER) { - print_error('invalidarguments'); -} - -$userid = $context->instanceid; - -switch ($filearea) { - case 'user_profile': - require_login(); - if (isguestuser()) { - print_error('noguest'); - } - - // access controll here must match user edit forms - if ($userid == $USER->id) { - if (!has_capability('moodle/user:editownprofile', get_context_instance(CONTEXT_SYSTEM))) { - send_file_not_found(); - } - } else { - if (!has_capability('moodle/user:editprofile', $context) and !has_capability('moodle/user:update', $context)) { - send_file_not_found(); - } - } - $itemid = 0; - $forcedownload = true; - break; - - case 'user_private': - require_login(); - if (isguestuser()) { - send_file_not_found(); - } - if ($USER->id != $userid) { - send_file_not_found(); - } - $itemid = 0; - $forcedownload = true; - break; - case 'user_backup': - require_login(); - if (isguestuser()) { - send_file_not_found(); - } - if ($USER->id != $userid) { - send_file_not_found(); - } - $itemid = 0; - $forcedownload = true; - break; - - default: - send_file_not_found(); -} - -$relativepath = '/'.implode('/', $args); - -$fs = get_file_storage(); - -$fullpath = $context->id.$filearea.$itemid.$relativepath; - -if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->get_filename() == '.') { - send_file_not_found(); -} - -// ======================================== -// finally send the file -// ======================================== -session_get_instance()->write_close(); // unlock session during fileserving -send_stored_file($file, 0, false, $forcedownload); Index: index.php =================================================================== RCS file: /cvsroot/moodle/moodle/index.php,v retrieving revision 1.260 diff -u -r1.260 index.php --- index.php 23 Jun 2010 08:54:19 -0000 1.260 +++ index.php 28 Jun 2010 22:31:43 -0000 @@ -91,7 +91,7 @@ echo $OUTPUT->header(); /// Print Section - if ($SITE->numsections > 0) { + if ($SITE->numsections > 0) { if (!$section = $DB->get_record('course_sections', array('course'=>$SITE->id, 'section'=>1))) { $DB->delete_records('course_sections', array('course'=>$SITE->id, 'section'=>1)); // Just in case @@ -104,7 +104,7 @@ $section->id = $DB->insert_record('course_sections', $section); } - if (!empty($section->sequence) or !empty($section->summary) or $editing) { + if (!empty($section->sequence) or !empty($section->summary) or $editing) { echo $OUTPUT->box_start('generalbox sitetopic'); /// If currently moving a file then show the current clipboard @@ -116,7 +116,7 @@ } $context = get_context_instance(CONTEXT_COURSE, SITEID); - $summarytext = file_rewrite_pluginfile_urls($section->summary, 'pluginfile.php', $context->id, 'course_section', $section->id); + $summarytext = file_rewrite_pluginfile_urls($section->summary, 'pluginfile.php', $context->id, 'course', 'section', $section->id); $summaryformatoptions = new object(); $summaryformatoptions->noclean = true; @@ -139,13 +139,13 @@ } } - if (isloggedin() and !isguestuser() and isset($CFG->frontpageloggedin)) { + if (isloggedin() and !isguestuser() and isset($CFG->frontpageloggedin)) { $frontpagelayout = $CFG->frontpageloggedin; } else { $frontpagelayout = $CFG->frontpage; } - foreach (explode(',',$frontpagelayout) as $v) { + foreach (explode(',',$frontpagelayout) as $v) { switch ($v) { /// Display the main part of the front page. case FRONTPAGENEWS: if ($SITE->newsitems) { // Print forums only when needed @@ -177,7 +177,7 @@ } break; - case FRONTPAGECOURSELIST: + case FRONTPAGECOURSELIST: if (isloggedin() and !has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM)) and !isguestuser() and empty($CFG->disablemycourses)) { echo html_writer::tag('a', get_string('skipa', 'access', moodle_strtolower(get_string('mycourses'))), array('href'=>'#skipmycourses', 'class'=>'skip-block')); echo $OUTPUT->heading(get_string('mycourses'), 2, 'headingblock header'); @@ -189,7 +189,7 @@ echo $OUTPUT->heading(get_string('availablecourses'), 2, 'headingblock header'); print_courses(0); echo html_writer::tag('span', '', array('class'=>'skip-block-to', 'id'=>'skipavailablecourses')); - } + } break; case FRONTPAGECATEGORYNAMES: Index: version.php =================================================================== RCS file: /cvsroot/moodle/moodle/version.php,v retrieving revision 1.1632 diff -u -r1.1632 version.php --- version.php 28 Jun 2010 00:43:51 -0000 1.1632 +++ version.php 28 Jun 2010 22:31:44 -0000 @@ -6,7 +6,7 @@ // This is compared against the values stored in the database to determine // whether upgrades should be performed (see lib/db/*.php) - $version = 2010062101; // YYYYMMDD = date of the last version bump + $version = 2010062900; // YYYYMMDD = date of the last version bump // XX = daily increments $release = '2.0 Preview 3 (Build: 20100628)'; // Human-friendly version name Index: pluginfile.php =================================================================== RCS file: /cvsroot/moodle/moodle/pluginfile.php,v retrieving revision 1.29 diff -u -r1.29 pluginfile.php --- pluginfile.php 21 Jun 2010 15:31:01 -0000 1.29 +++ pluginfile.php 28 Jun 2010 22:31:44 -0000 @@ -18,14 +18,15 @@ /** * This script delegates file serving to individual plugins * - * @package moodlecore + * @package core * @subpackage file * @copyright 2008 Petr Skoda (http://skodak.org) * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ // disable moodle specific debug messages and any errors in output -define('NO_DEBUG_DISPLAY', true); +//define('NO_DEBUG_DISPLAY', true); +//TODO: uncomment this once the file api stabilises a bit more require_once('config.php'); require_once('lib/filelib.php'); @@ -43,16 +44,16 @@ // extract relative path components $args = explode('/', ltrim($relativepath, '/')); -if (count($args) == 0) { // always at least user id +if (count($args) < 3) { // always at least context, component and filearea print_error('invalidarguments'); } $contextid = (int)array_shift($args); -$filearea = array_shift($args); +$component = clean_param(array_shift($args), PARAM_SAFEDIR); +$filearea = clean_param(array_shift($args), PARAM_SAFEDIR); + +list($context, $course, $cm) = get_context_info_array($contextid); -if (!$context = get_context_instance_by_id($contextid)) { - send_file_not_found(); -} $fs = get_file_storage(); // If the file is a Flash file and that the user flash player is outdated return a flash upgrader MDL-20841 @@ -60,116 +61,158 @@ if (!empty($CFG->excludeoldflashclients) && $mimetype == 'application/x-shockwave-flash'&& !empty($SESSION->flashversion)) { $userplayerversion = explode('.', $SESSION->flashversion); $requiredplayerversion = explode('.', $CFG->excludeoldflashclients); - $sendflashupgrader = true; -} -if (!empty($sendflashupgrader) && (($userplayerversion[0] < $requiredplayerversion[0]) || + if (($userplayerversion[0] < $requiredplayerversion[0]) || ($userplayerversion[0] == $requiredplayerversion[0] && $userplayerversion[1] < $requiredplayerversion[1]) || ($userplayerversion[0] == $requiredplayerversion[0] && $userplayerversion[1] == $requiredplayerversion[1] - && $userplayerversion[2] < $requiredplayerversion[2]))) { + && $userplayerversion[2] < $requiredplayerversion[2])) { $path = $CFG->dirroot."/lib/flashdetect/flashupgrade.swf"; // Alternate content asking user to upgrade Flash $filename = "flashupgrade.swf"; - $lifetime = 0; // Do not cache - send_file($path, $filename, $lifetime, 0, false, false, $mimetype); + send_file($path, $filename, O, 0, false, false, 'application/x-shockwave-flash'); // Do not cache + } +} -} else if ($context->contextlevel == CONTEXT_SYSTEM) { - if ($filearea === 'blog_attachment' || $filearea === 'blog_post') { +// ======================================================================================================================== +if ($component === 'blog') { + // Blog file serving + if ($context->contextlevel != CONTEXT_SYSTEM) { + send_file_not_found(); + } + if ($filearea !== 'attachment' and $filearea !== 'post') { + send_file_not_found(); + } - if (empty($CFG->bloglevel)) { - print_error('siteblogdisable', 'blog'); - } - if ($CFG->bloglevel < BLOG_GLOBAL_LEVEL) { - require_login(); - if (isguestuser()) { - print_error('noguest'); - } - if ($CFG->bloglevel == BLOG_USER_LEVEL) { - if ($USER->id != $entry->userid) { - send_file_not_found(); - } - } - } - $entryid = (int)array_shift($args); - if (!$entry = $DB->get_record('post', array('module'=>'blog', 'id'=>$entryid))) { - send_file_not_found(); - } - if ('publishstate' === 'public') { - if ($CFG->forcelogin) { - require_login(); - } + if (empty($CFG->bloglevel)) { + print_error('siteblogdisable', 'blog'); + } - } else if ('publishstate' === 'site') { - require_login(); - //ok - } else if ('publishstate' === 'draft') { - require_login(); + if ($CFG->bloglevel < BLOG_GLOBAL_LEVEL) { + require_login(); + if (isguestuser()) { + print_error('noguest'); + } + if ($CFG->bloglevel == BLOG_USER_LEVEL) { if ($USER->id != $entry->userid) { send_file_not_found(); } } + } + $entryid = (int)array_shift($args); + if (!$entry = $DB->get_record('post', array('module'=>'blog', 'id'=>$entryid))) { + send_file_not_found(); + } - //TODO: implement shared course and shared group access - - $relativepath = '/'.implode('/', $args); - $fullpath = $context->id.$filearea.$entryid.$relativepath; + if ('publishstate' === 'public') { + if ($CFG->forcelogin) { + require_login(); + } - if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) { + } else if ('publishstate' === 'site') { + require_login(); + //ok + } else if ('publishstate' === 'draft') { + require_login(); + if ($USER->id != $entry->userid) { send_file_not_found(); } + } + + $filename = array_pop($args); + $filepath = '/'.implode('/', $args); + + if (!$file = $fs->get_file($context->id, $component, $filearea, $entryid, $filepath, $filename) or $file->is_directory()) { + send_file_not_found(); + } - send_stored_file($file, 10*60, 0, true); // download MUST be forced - security! - } else if ($filearea === 'grade_outcome' || $filearea === 'grade_scale') { // CONTEXT_SYSTEM + send_stored_file($file, 10*60, 0, true); // download MUST be forced - security! + +// ======================================================================================================================== +} else if ($component === 'grade') { + if (($filearea === 'outcome' or $filearea === 'scale') and $context->contextlevel == CONTEXT_SYSTEM) { + // Global gradebook files if ($CFG->forcelogin) { require_login(); } - $fullpath = $context->id.$filearea.implode('/', $args); + $fullpath = "/$context->id/$component/$filearea/".implode('/', $args); if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) { send_file_not_found(); } session_get_instance()->write_close(); // unlock session during fileserving - send_stored_file($file, 60*60, 0, $forcedownload); // TODO: change timeout? + send_stored_file($file, 60*60, 0, $forcedownload); - } else if ($filearea === 'tag_description') { // CONTEXT_SYSTEM + } else if ($filearea === 'feedback' and $context->contextlevel == CONTEXT_COURSE) { + //TODO: nobody implemented this yet in grade edit form!! + send_file_not_found(); - // All tag descriptions are going to be public but we still need to respect forcelogin - if ($CFG->forcelogin) { - require_login(); + if ($CFG->forcelogin || $course->id !== SITEID) { + require_login($course); } - $fullpath = $context->id.$filearea.implode('/', $args); + $fullpath = "/$context->id/$component/$filearea/".implode('/', $args); if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) { send_file_not_found(); } session_get_instance()->write_close(); // unlock session during fileserving - send_stored_file($file, 60*60, 0, true); // TODO: change timeout? + send_stored_file($file, 60*60, 0, $forcedownload); + } else { + send_file_not_found(); + } - } else if ($filearea === 'calendar_event_description') { // CONTEXT_SYSTEM +// ======================================================================================================================== +} else if ($component === 'tag') { + if ($filearea === 'description' and $context->contextlevel == CONTEXT_SYSTEM) { - // All events here are public the one requirement is that we respect forcelogin + // All tag descriptions are going to be public but we still need to respect forcelogin if ($CFG->forcelogin) { require_login(); } - $fullpath = $context->id.$filearea.implode('/', $args); + $fullpath = "/$context->id/tage/description/".implode('/', $args); if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) { send_file_not_found(); } session_get_instance()->write_close(); // unlock session during fileserving - send_stored_file($file, 60*60, 0, $forcedownload); // TODO: change timeout? + send_stored_file($file, 60*60, 0, true); } else { send_file_not_found(); } -} else if ($context->contextlevel == CONTEXT_USER) { +// ======================================================================================================================== +} else if ($component === 'calendar') { + if ($filearea === 'event_description' and $context->contextlevel == CONTEXT_SYSTEM) { - if ($filearea === 'calendar_event_description') { // CONTEXT_USER + // All events here are public the one requirement is that we respect forcelogin + if ($CFG->forcelogin) { + require_login(); + } + + // Get the event if from the args array + $eventid = array_shift($args); + + // Load the event from the database + if (!$event = $DB->get_record('event', array('id'=>(int)$eventid, 'eventtype'=>'site'))) { + send_file_not_found(); + } + // Check that we got an event and that it's userid is that of the user + + // Get the file and serve if successful + $filename = array_pop($args); + $filepath = '/'.implode('/', $args); + if (!$file = $fs->get_file($context->id, $component, $filearea, $eventid, $filepath, $filename) or $file->is_directory()) { + send_file_not_found(); + } + + session_get_instance()->write_close(); // unlock session during fileserving + send_stored_file($file, 60*60, 0, $forcedownload); + + } else if ($filearea === 'event_description' and $context->contextlevel == CONTEXT_USER) { // Must be logged in, if they are not then they obviously can't be this user require_login(); @@ -181,209 +224,248 @@ // Get the event if from the args array $eventid = array_shift($args); - if ((int)$eventid <= 0) { - send_file_not_found(); - } - // Load the event from the database - $event = $DB->get_record('event', array('id'=>(int)$eventid)); - // Check that we got an event and that it's userid is that of the user - if (!$event || $event->userid !== $USER->id) { + // Load the event from the database - user id must match + if (!$event = $DB->get_record('event', array('id'=>(int)$eventid, 'userid'=>$USER->id, 'eventtype'=>'user'))) { send_file_not_found(); } - // Get the file and serve if succesfull - $fullpath = $context->id.$filearea.$eventid.'/'.implode('/', $args); - if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) { + // Get the file and serve if successful + $filename = array_pop($args); + $filepath = '/'.implode('/', $args); + if (!$file = $fs->get_file($context->id, $component, $filearea, $eventid, $filepath, $filename) or $file->is_directory()) { send_file_not_found(); } session_get_instance()->write_close(); // unlock session during fileserving - send_stored_file($file, 60*60, 0, $forcedownload); // TODO: change timeout? - } else if ($filearea === 'user_profile') { // CONTEXT_USER + send_stored_file($file, 60*60, 0, $forcedownload); - if ($CFG->forcelogin) { - require_login(); + } else if ($filearea === 'event_description' and $context->contextlevel == CONTEXT_COURSE) { + + // Respect forcelogin and require login unless this is the site.... it probably + // should NEVER be the site + if ($CFG->forcelogin || $course->id !== SITEID) { + require_login($course); } - $userid = array_shift($args); - if ((int)$userid <= 0) { + // Must be able to at least view the course + if (!is_enrolled($context) and !is_viewing($context)) { + //TODO: hmm, do we really want to block guests here? send_file_not_found(); } - if (!empty($CFG->forceloginforprofiles)) { - require_login(); - if (isguestuser()) { - send_file_not_found(); - } - - if ($USER->id !== $userid) { - $usercontext = get_context_instance(CONTEXT_USER, $userid); - // The browsing user is not the current user - if (!has_coursecontact_role($userid) && !has_capability('moodle/user:viewdetails', $usercontext)) { - send_file_not_found(); - } + // Get the event id + $eventid = array_shift($args); - $canview = false; - if (has_capability('moodle/user:viewdetails', $usercontext)) { - $canview = true; - } else { - $courses = enrol_get_my_courses(); - } + // Load the event from the database we need to check whether it is + // a) valid course event + // b) a group event + // Group events use the course context (there is no group context) + if (!$event = $DB->get_record('event', array('id'=>(int)$eventid, 'courseid'=>$course->id))) { + send_file_not_found(); + } - while (!$canview && count($courses) > 0) { - $course = array_shift($courses); - if (has_capability('moodle/user:viewdetails', get_context_instance(CONTEXT_COURSE, $course->id))) { - $canview = true; - } - } + // If its a group event require either membership of view all groups capability + if ($event->eventtype === 'group') { + if (!has_capability('moodle/site:accessallgroups', $context) && !groups_is_member($event->groupid, $USER->id)) { + send_file_not_found(); } + } else if ($event->eventtype === 'course') { + //ok + } else { + // some other type + send_file_not_found(); } - $fullpath = $context->id.$filearea.$userid.'/'.implode('/', $args); - - if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) { + // If we get this far we can serve the file + $filename = array_pop($args); + $filepath = '/'.implode('/', $args); + if (!$file = $fs->get_file($context->id, $component, $filearea, $eventid, $filepath, $filename) or $file->is_directory()) { send_file_not_found(); } session_get_instance()->write_close(); // unlock session during fileserving - send_stored_file($file, 60*60, 0, true); - } + send_stored_file($file, 60*60, 0, $forcedownload); - send_file_not_found(); + } else { + send_file_not_found(); + } +// ======================================================================================================================== +} else if ($component === 'user') { + if ($filearea === 'private' and $context->contextlevel == CONTEXT_USER) { + require_login(); -} else if ($context->contextlevel == CONTEXT_COURSECAT) { - if ($filearea == 'coursecat_intro') { - if ($CFG->forcelogin) { - // no login necessary - unless login forced everywhere - require_login(); + if (isguestuser()) { + send_file_not_found(); } - $relativepath = '/'.implode('/', $args); - $fullpath = $context->id.'coursecat_intro0'.$relativepath; + if ($USER->id !== $context->instanceid) { + send_file_not_found(); + } - if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->get_filename() == '.') { + $filename = array_pop($args); + $filepath = '/'.implode('/', $args); + if (!$file = $fs->get_file($context->id, $component, $filearea, 0, $filepath, $filename) or $file->is_directory()) { send_file_not_found(); } session_get_instance()->write_close(); // unlock session during fileserving - send_stored_file($file, 60*60, 0, $forcedownload); - } else if ($filearea == 'category_description') { + send_stored_file($file, 0, 0, true); // must force download - security! + + } else if ($filearea === 'profile' and $context->contextlevel == CONTEXT_USER) { + if ($CFG->forcelogin) { - // no login necessary - unless login forced everywhere require_login(); } - $itemid = (int)array_shift($args); - $relativepath = '/'.implode('/', $args); - $fullpath = $context->id.'category_description'.$itemid.$relativepath; + $userid = $context->instanceid; - if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->get_filename() == '.') { - send_file_not_found(); - } + if ($USER->id == $userid) { + // always can access own - session_get_instance()->write_close(); // unlock session during fileserving - send_stored_file($file, 60*60, 0, $forcedownload); - } else { - send_file_not_found(); - } + } else if (!empty($CFG->forceloginforprofiles)) { + require_login(); + if (isguestuser()) { + send_file_not_found(); + } -} else if ($context->contextlevel == CONTEXT_COURSE) { - if (!$course = $DB->get_record('course', array('id'=>$context->instanceid))) { - print_error('invalidcourseid'); - } + // we allow access to site profile of all course contacts (usually teachers) + if (!has_coursecontact_role($userid) && !has_capability('moodle/user:viewdetails', $context)) { + send_file_not_found(); + } - if ($filearea === 'course_backup') { - require_login($course); - require_capability('moodle/backup:downloadfile', $context); + $canview = false; + if (has_capability('moodle/user:viewdetails', $context)) { + $canview = true; + } else { + $courses = enrol_get_my_courses(); + } - $relativepath = '/'.implode('/', $args); - $fullpath = $context->id.'course_backup0'.$relativepath; + while (!$canview && count($courses) > 0) { + $course = array_shift($courses); + if (has_capability('moodle/user:viewdetails', get_context_instance(CONTEXT_COURSE, $course->id))) { + $canview = true; + } + } + } - if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) { + $filename = array_pop($args); + $filepath = '/'.implode('/', $args); + if (!$file = $fs->get_file($context->id, $component, $filearea, 0, $filepath, $filename) or $file->is_directory()) { send_file_not_found(); } session_get_instance()->write_close(); // unlock session during fileserving - send_stored_file($file, 0, 0, true); + send_stored_file($file, 0, 0, true); // must force download - security! + + } else if ($filearea === 'profile' and $context->contextlevel == CONTEXT_COURSE) { + $userid = (int)array_shift($args); + $usercontext = get_context_instance(CONTEXT_USER, $userid); - } else if ($filearea === 'course_summary') { if ($CFG->forcelogin) { require_login(); } - $relativepath = '/'.implode('/', $args); - $fullpath = $context->id.'course_summary0'.$relativepath; + if (!empty($CFG->forceloginforprofiles)) { + require_login(); + if (isguestuser()) { + print_error('noguest'); + } - if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) { + //TODO: review this logic of user profile access prevention + if (!has_coursecontact_role($userid) and !has_capability('moodle/user:viewdetails', $usercontext)) { + print_error('usernotavailable'); + } + if (!has_capability('moodle/user:viewdetails', $context) && !has_capability('moodle/user:viewdetails', $usercontext)) { + print_error('cannotviewprofile'); + } + if (!is_enrolled($context, $userid)) { + print_error('notenrolledprofile'); + } + if (groups_get_course_groupmode($course) == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) { + print_error('groupnotamember'); + } + } + + $filename = array_pop($args); + $filepath = '/'.implode('/', $args); + if (!$file = $fs->get_file($context->id, 'user', 'profile', 0, $filepath, $filename) or $file->is_directory()) { send_file_not_found(); } session_get_instance()->write_close(); // unlock session during fileserving - send_stored_file($file, 60*60, 0, $forcedownload); // TODO: change timeout? + send_stored_file($file, 0, 0, true); // must force download - security! - } else if ($filearea === 'course_grade_tree_feedback') { + } else if ($filearea === 'backup' and $context->contextlevel == CONTEXT_USER) { + require_login(); - if ($CFG->forcelogin || $course->id !== SITEID) { - require_login($course); + if (isguestuser()) { + send_file_not_found(); + } + if ($USER->id != $userid) { + send_file_not_found(); } - $fullpath = $context->id.$filearea.implode('/', $args); - - if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) { + $filename = array_pop($args); + $filepath = '/'.implode('/', $args); + if (!$file = $fs->get_file($context->id, 'user', 'backup', 0, $filepath, $filename) or $file->is_directory()) { send_file_not_found(); } session_get_instance()->write_close(); // unlock session during fileserving - send_stored_file($file, 60*60, 0, $forcedownload); // TODO: change timeout? + send_stored_file($file, 0, 0, true); // must force download - security! - } else if ($filearea === 'calendar_event_description') { // CONTEXT_COURSE + } else { + send_file_not_found(); + } - // This is for content used in course and group events +// ======================================================================================================================== +} else if ($component === 'coursecat') { + if ($context->contextlevel != CONTEXT_COURSECAT) { + send_file_not_found(); + } - // Respect forcelogin and require login unless this is the site.... it probably - // should NEVER be the site - if ($CFG->forcelogin || $course->id !== SITEID) { - require_login($course); + if ($filearea === 'description') { + if ($CFG->forcelogin) { + // no login necessary - unless login forced everywhere + require_login(); } - // Must be able to at least view the course - if (!is_enrolled($context) and !is_viewing($context)) { + $filename = array_pop($args); + $filepath = '/'.implode('/', $args); + if (!$file = $fs->get_file($context->id, 'coursecat', 'description', 0, $filepath, $filename) or $file->is_directory()) { send_file_not_found(); } - // Get the event id - $eventid = array_shift($args); - if ((int)$eventid <= 0) { - send_file_not_found(); - } + session_get_instance()->write_close(); // unlock session during fileserving + send_stored_file($file, 60*60, 0, $forcedownload); + } else { + send_file_not_found(); + } - // Load the event from the database we need to check whether it is - // a) valid - // b) a group event - // Group events use the course context (there is no group context) - $event = $DB->get_record('event', array('id'=>(int)$eventid)); - if (!$event || $event->userid !== $USER->id) { - send_file_not_found(); - } +// ======================================================================================================================== +} else if ($component === 'course') { + if ($context->contextlevel != CONTEXT_COURSE) { + send_file_not_found(); + } - // If its a group event require either membership of manage groups capability - if ($event->eventtype === 'group' && !has_capability('moodle/course:managegroups', $context) && !groups_is_member($event->groupid, $USER->id)) { - send_file_not_found(); + if ($filearea === 'summary') { + if ($CFG->forcelogin) { + require_login(); } - // If we get this far we can serve the file - $fullpath = $context->id.$filearea.$eventid.'/'.implode('/', $args); - if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) { + $filename = array_pop($args); + $filepath = '/'.implode('/', $args); + if (!$file = $fs->get_file($context->id, 'course', 'summary', 0, $filepath, $filename) or $file->is_directory()) { send_file_not_found(); } session_get_instance()->write_close(); // unlock session during fileserving - send_stored_file($file, 60*60, 0, $forcedownload); // TODO: change timeout? + send_stored_file($file, 60*60, 0, $forcedownload); - } else if ($filearea === 'course_section') { + } else if ($filearea === 'section') { if ($CFG->forcelogin) { require_login($course); } else if ($course->id !== SITEID) { @@ -400,115 +482,147 @@ } } - $relativepath = '/'.implode('/', $args); - $fullpath = $context->id.'course_section'.$sectionid.$relativepath; - - if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) { + $filename = array_pop($args); + $filepath = '/'.implode('/', $args); + if (!$file = $fs->get_file($context->id, 'course', 'section', $sectionid, $filepath, $filename) or $file->is_directory()) { send_file_not_found(); } session_get_instance()->write_close(); // unlock session during fileserving - send_stored_file($file, 60*60, 0, false); // TODO: change timeout? + send_stored_file($file, 60*60, 0, $forcedownload); - } else if ($filearea === 'section_backup') { - require_login($course); - require_capability('moodle/backup:downloadfile', $context); + } else { + send_file_not_found(); + } - $sectionid = (int)array_shift($args); +} else if ($component === 'group') { + if ($context->contextlevel != CONTEXT_COURSE) { + send_file_not_found(); + } - $relativepath = '/'.implode('/', $args); - $fullpath = $context->id.'section_backup'.$sectionid.$relativepath; + require_login($course); - if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) { + $groupid = (int)array_shift($args); + + $group = $DB->get_record('groups', array('id'=>$groupid, 'courseid'=>$course->id), '*', MUST_EXIST); + if (!has_capability('moodle/site:accessallgroups', $context) && !groups_is_member($group->id, $USER->id)) { + send_file_not_found(); + } + + if ($filearea === 'description' or $filearea === 'icon') { + + //TODO: implement group image storage in file pool + + $filename = array_pop($args); + $filepath = '/'.implode('/', $args); + if (!$file = $fs->get_file($context->id, 'group', 'description', $group->id, $filepath, $filename) or $file->is_directory()) { send_file_not_found(); } - session_get_instance()->write_close(); - send_stored_file($file, 60*60, 0, false); + session_get_instance()->write_close(); // unlock session during fileserving + send_stored_file($file, 60*60, 0, $forcedownload); - } else if ($filearea === 'user_profile') { - $userid = (int)array_shift($args); - $usercontext = get_context_instance(CONTEXT_USER, $userid); + } else { + send_file_not_found(); + } - if ($CFG->forcelogin) { - require_login(); - } +} else if ($component === 'grouping') { + if ($context->contextlevel != CONTEXT_COURSE) { + send_file_not_found(); + } - if (!empty($CFG->forceloginforprofiles)) { - require_login(); - if (isguestuser()) { - print_error('noguest'); - } + require_login($course); - if (!has_coursecontact_role($userid) and !has_capability('moodle/user:viewdetails', $usercontext)) { - print_error('usernotavailable'); - } - if (!has_capability('moodle/user:viewdetails', $context) && - !has_capability('moodle/user:viewdetails', $usercontext)) { - print_error('cannotviewprofile'); - } - if (!is_enrolled($context, $userid)) { - print_error('notenrolledprofile'); - } - if (groups_get_course_groupmode($course) == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) { - print_error('groupnotamember'); - } - } + $groupingid = (int)array_shift($args); - $relativepath = '/'.implode('/', $args); - $fullpath = $usercontext->id.'user_profile0'.$relativepath; + // note: everybody has access to grouping desc images for now + if ($filearea === 'description') { - if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) { + $filename = array_pop($args); + $filepath = '/'.implode('/', $args); + if (!$file = $fs->get_file($context->id, 'grouping', 'description', $groupingid->id, $filepath, $filename) or $file->is_directory()) { send_file_not_found(); } session_get_instance()->write_close(); // unlock session during fileserving - send_stored_file($file, 0, 0, true); // must force download - security! + send_stored_file($file, 60*60, 0, $forcedownload); } else { send_file_not_found(); } -} else if ($context->contextlevel == CONTEXT_MODULE) { +// ======================================================================================================================== +} else if ($component === 'backup') { + if ($filearea === 'course' and $context->contextlevel == CONTEXT_COURSE) { + require_login($course); + require_capability('moodle/backup:downloadfile', $context); - if (!$coursecontext = get_context_instance_by_id(get_parent_contextid($context))) { - send_file_not_found(); - } + $filename = array_pop($args); + $filepath = '/'.implode('/', $args); + if (!$file = $fs->get_file($context->id, 'backup', 'course', 0, $filepath, $filename) or $file->is_directory()) { + send_file_not_found(); + } - if (!$course = $DB->get_record('course', array('id'=>$coursecontext->instanceid))) { - send_file_not_found(); - } - $modinfo = get_fast_modinfo($course); - if (empty($modinfo->cms[$context->instanceid])) { + session_get_instance()->write_close(); // unlock session during fileserving + send_stored_file($file, 0, 0, $forcedownload); + + } else if ($filearea === 'section' and $context->contextlevel == CONTEXT_COURSE) { + require_login($course); + require_capability('moodle/backup:downloadfile', $context); + + $sectionid = (int)array_shift($args); + + $filename = array_pop($args); + $filepath = '/'.implode('/', $args); + if (!$file = $fs->get_file($context->id, 'backup', 'section', $sectionid, $filepath, $filename) or $file->is_directory()) { + send_file_not_found(); + } + + session_get_instance()->write_close(); + send_stored_file($file, 60*60, 0, $forcedownload); + + } else if ($filearea === 'activity' and $context->contextlevel == CONTEXT_MODULE) { + require_login($course, false, $cm); + require_capability('moodle/backup:downloadfile', $context); + + $filename = array_pop($args); + $filepath = '/'.implode('/', $args); + if (!$file = $fs->get_file($context->id, 'backup', 'activity', 0, $filepath, $filename) or $file->is_directory()) { + send_file_not_found(); + } + + session_get_instance()->write_close(); + send_stored_file($file, 60*60, 0, $forcedownload); + + } else { send_file_not_found(); } - $cminfo = $modinfo->cms[$context->instanceid]; - $modname = $cminfo->modname; - $libfile = "$CFG->dirroot/mod/$modname/lib.php"; - if (!file_exists($libfile)) { +// ======================================================================================================================== +} else if (strpos($component, 'mod_') === 0) { + $modname = substr($component, 4); + if (!file_exists("$CFG->dirroot/mod/$modname/lib.php")) { send_file_not_found(); } + require_once("$CFG->dirroot/mod/$modname/lib.php"); - require_once($libfile); - if ($filearea === $modname.'_intro') { - if (!plugin_supports('mod', $modname, FEATURE_MOD_INTRO, true)) { + if ($context->contextlevel == CONTEXT_MODULE) { + if ($cm->modname !== $modname) { + // somebody tries to gain illegal access, cm type must match the component! send_file_not_found(); } - if (!$cm = get_coursemodule_from_instance($modname, $cminfo->instance, $course->id)) { + } + + if ($filearea === 'intro') { + if (!plugin_supports('mod', $modname, FEATURE_MOD_INTRO, true)) { send_file_not_found(); } require_course_login($course, true, $cm); - if (!$cminfo->uservisible) { - send_file_not_found(); - } // all users may access it - $relativepath = '/'.implode('/', $args); - $fullpath = $context->id.$filearea.'0'.$relativepath; - - $fs = get_file_storage(); - if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) { + $filename = array_pop($args); + $filepath = '/'.implode('/', $args); + if (!$file = $fs->get_file($context->id, 'mod_'.$modname, 'intro', 0, $filepath, $filename) or $file->is_directory()) { send_file_not_found(); } @@ -516,51 +630,64 @@ // finally send the file send_stored_file($file, $lifetime, 0); - } else if ($filearea === 'activity_backup') { - require_login($course); - require_capability('moodle/backup:downloadfile', $context); - - $relativepath = '/'.implode('/', $args); - $fullpath = $context->id.'activity_backup0'.$relativepath; - - if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) { - send_file_not_found(); - } - - session_get_instance()->write_close(); - send_stored_file($file, 60*60, 0, false); } - $filefunction = $modname.'_pluginfile'; + $filefunction = $component.'_pluginfile'; + $filefunctionold = $modname.'_pluginfile'; if (function_exists($filefunction)) { // if the function exists, it must send the file and terminate. Whatever it returns leads to "not found" - $filefunction($course, $cminfo, $context, $filearea, $args, $forcedownload); + $filefunction($course, $cm, $context, $filearea, $args, $forcedownload); + } else if (function_exists($filefunctionold)) { + // if the function exists, it must send the file and terminate. Whatever it returns leads to "not found" + $filefunctionold($course, $cm, $context, $filearea, $args, $forcedownload); } send_file_not_found(); -} else if ($context->contextlevel == CONTEXT_BLOCK) { - - if (!$context = get_context_instance_by_id($contextid)) { +// ======================================================================================================================== +} else if (strpos($component, 'block_') === 0) { + $blockname = substr($component, 6); + // note: no more class methods in blocks please, that is .... + if (!file_exists("$CFG->dirroot/blocks/$blockname/lib.php")) { send_file_not_found(); } - $birecord = $DB->get_record('block_instances', array('id'=>$context->instanceid), '*',MUST_EXIST); - $blockinstance = block_instance($birecord->blockname, $birecord); + require_once("$CFG->dirroot/blocks/$blockname/lib.php"); - if (strpos(get_class($blockinstance), $filearea) !== 0) { - send_file_not_found(); + if ($context->contextlevel == CONTEXT_BLOCK) { + $birecord = $DB->get_record('block_instances', array('id'=>$context->instanceid), '*',MUST_EXIST); + if ($birecord->blockname !== $blockname) { + // somebody tries to gain illegal access, cm type must match the component! + send_file_not_found(); + } + } else { + $birecord = null; } - $itemid = array_shift($args); - $filename = array_pop($args); - $filepath = '/'.join('/', $args); - - if (method_exists($blockinstance, 'send_file')) { - $blockinstance->send_file($context, $filearea, $itemid, $filepath, $filename); + $filefunction = $component.'_pluginfile'; + if (function_exists($filefunction)) { + // if the function exists, it must send the file and terminate. Whatever it returns leads to "not found" + $filefunction($course, $birecord, $context, $filearea, $args, $forcedownload); } send_file_not_found(); +} else if (strpos($component, '_') === false) { + // all core subsystems have to be specified above, no more guessing here! + send_file_not_found(); + } else { + // try to serve general plugin file in arbitrary context + $dir = get_component_directory($component); + if (!file_exists("$dir/lib.php")) { + send_file_not_found(); + } + require_once("$dir/lib.php"); + + $filefunction = $component.'_pluginfile'; + if (function_exists($filefunction)) { + // if the function exists, it must send the file and terminate. Whatever it returns leads to "not found" + $filefunction($course, $cm, $context, $filearea, $args, $forcedownload); + } + send_file_not_found(); } Index: mod/label/backup/moodle2/backup_label_stepslib.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/label/backup/moodle2/backup_label_stepslib.php,v retrieving revision 1.1 diff -u -r1.1 backup_label_stepslib.php --- mod/label/backup/moodle2/backup_label_stepslib.php 4 May 2010 20:21:13 -0000 1.1 +++ mod/label/backup/moodle2/backup_label_stepslib.php 28 Jun 2010 22:32:51 -0000 @@ -50,7 +50,7 @@ // (none) // Define file annotations - $label->annotate_files(array('label_intro'), null); // This file area hasn't itemid + $label->annotate_files('mod_label', 'intro', null); // This file area hasn't itemid // Return the root element (label), wrapped into standard activity structure return $this->prepare_activity_structure($label); Index: mod/assignment/lib.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/assignment/lib.php,v retrieving revision 1.460 diff -u -r1.460 lib.php --- mod/assignment/lib.php 28 Jun 2010 14:10:33 -0000 1.460 +++ mod/assignment/lib.php 28 Jun 2010 22:32:34 -0000 @@ -1161,7 +1161,7 @@ echo ''; return true; } - if ($this->assignment->assignmenttype=='upload' || $this->assignment->assignmenttype=='online' || $this->assignment->assignmenttype=='uploadsingle') { + if ($this->assignment->assignmenttype=='upload' || $this->assignment->assignmenttype=='online' || $this->assignment->assignmenttype=='uploadsingle') { //TODO: this is an ugly hack, where is the plugin spirit? (skodak) echo ''; } /// Construct the SQL @@ -1723,11 +1723,10 @@ $output = ''; $fs = get_file_storage(); - $browser = get_file_browser(); $found = false; - if ($files = $fs->get_area_files($this->context->id, 'assignment_submission', $userid, "timemodified", false)) { + if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $userid, "timemodified", false)) { require_once($CFG->libdir.'/portfoliolib.php'); require_once($CFG->dirroot . '/mod/assignment/locallib.php'); $button = new portfolio_add_button(); @@ -1735,7 +1734,7 @@ $filename = $file->get_filename(); $found = true; $mimetype = $file->get_mimetype(); - $path = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/assignment_submission/'.$userid.'/'.$filename); + $path = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/mod_assignment/submission/'.$userid.'/'.$filename); $output .= ''.$mimetype.''.s($filename).''; if ($this->portfolio_exportable() && has_capability('mod/assignment:exportownsubmission', $this->context)) { $button->set_callback_options('assignment_portfolio_caller', array('id' => $this->cm->id, 'fileid' => $file->get_id()), '/mod/assignment/locallib.php'); @@ -1765,7 +1764,7 @@ */ function count_user_files($userid) { $fs = get_file_storage(); - $files = $fs->get_area_files($this->context->id, 'assignment_submission', $userid, "id", false); + $files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $userid, "id", false); return count($files); } @@ -1833,9 +1832,8 @@ if ($submission = $this->get_submission($user->id)) { $fs = get_file_storage(); - $browser = get_file_browser(); - if ($files = $fs->get_area_files($this->context->id, 'assignment_submission', $user->id, "timemodified", false)) { + if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $user->id, "timemodified", false)) { $countfiles = count($files)." ".get_string("uploadedfiles", "assignment"); foreach ($files as $file) { $countfiles .= "; ".$file->get_filename(); @@ -1942,8 +1940,8 @@ continue; } $context = get_context_instance(CONTEXT_MODULE, $cm->id); - $fs->delete_area_files($context->id, 'assignment_submission'); - $fs->delete_area_files($context->id, 'assignment_response'); + $fs->delete_area_files($context->id, 'mod_assignment', 'submission'); + $fs->delete_area_files($context->id, 'mod_assignment', 'response'); } } @@ -2063,7 +2061,7 @@ } -class mod_assignment_online_grading_form extends moodleform { +class mod_assignment_online_grading_form extends moodleform { // TODO: why "online" in the name of this class? (skodak) function definition() { global $OUTPUT; @@ -2213,9 +2211,10 @@ protected function get_editor_options() { $editoroptions = array(); - $editoroptions['filearea'] = 'assignment_online_submission'; + $editoroptions['component'] = 'mod_assignment'; + $editoroptions['filearea'] = 'feedback'; $editoroptions['noclean'] = false; - $editoroptions['maxfiles'] = 0; //TODO: no files for now, we need to first implement assignment_feedback area + $editoroptions['maxfiles'] = 0; //TODO: no files for now, we need to first implement assignment_feedback area, integration with gradebook, files support in quickgrading, etc. (skodak) $editoroptions['maxbytes'] = $this->_customdata->maxbytes; return $editoroptions; } @@ -2237,7 +2236,7 @@ $itemid = null; } - $data = file_prepare_standard_editor($data, 'submissioncomment', $editoroptions, $this->_customdata->context, $editoroptions['filearea'], $itemid); + $data = file_prepare_standard_editor($data, 'submissioncomment', $editoroptions, $this->_customdata->context, $editoroptions['component'], $editoroptions['filearea'], $itemid); return parent::set_data($data); } @@ -2247,12 +2246,12 @@ if (!empty($this->_customdata->submission->id)) { $itemid = $this->_customdata->submission->id; } else { - $itemid = null; + $itemid = null; //TODO: this is wrong, itemid MUST be known when saving files!! (skodak) } if ($data) { $editoroptions = $this->get_editor_options(); - $data = file_postupdate_standard_editor($data, 'submissioncomment', $editoroptions, $this->_customdata->context, $editoroptions['filearea'], $itemid); + $data = file_postupdate_standard_editor($data, 'submissioncomment', $editoroptions, $this->_customdata->context, $editoroptions['component'], $editoroptions['filearea'], $itemid); $data->format = $data->textformat; } return $data; @@ -2654,28 +2653,29 @@ } /** - * Serves assingment submissions and otehr files. + * Serves assignment submissions and other files. * * @param object $course - * @param object $cminfo + * @param object $cm * @param object $context * @param string $filearea * @param array $args * @param bool $forcedownload - * @return bool false if file not found, does not return if found - justsend the file + * @return bool false if file not found, does not return if found - just send the file */ -function assignment_pluginfile($course, $cminfo, $context, $filearea, $args, $forcedownload) { +function assignment_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload) { global $CFG, $DB; - if (!$assignment = $DB->get_record('assignment', array('id'=>$cminfo->instance))) { - return false; - } - if (!$cm = get_coursemodule_from_instance('assignment', $assignment->id, $course->id)) { + if ($context->contextlevel != CONTEXT_MODULE) { return false; } require_login($course, false, $cm); + if (!$assignment = $DB->get_record('assignment', array('id'=>$cm->instance))) { + return false; + } + require_once($CFG->dirroot.'/mod/assignment/type/'.$assignment->assignmenttype.'/assignment.class.php'); $assignmentclass = 'assignment_'.$assignment->assignmenttype; $assignmentinstance = new $assignmentclass($cm->id, $assignment, $cm, $course); @@ -3571,7 +3571,7 @@ function assignment_get_file_areas($course, $cm, $context) { $areas = array(); if (has_capability('moodle/course:managefiles', $context)) { - $areas['assignment_submission'] = get_string('assignmentsubmission', 'assignment'); + $areas['submission'] = get_string('assignmentsubmission', 'assignment'); } return $areas; } Index: mod/assignment/locallib.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/assignment/locallib.php,v retrieving revision 1.5 diff -u -r1.5 locallib.php --- mod/assignment/locallib.php 28 Apr 2010 02:16:10 -0000 1.5 +++ mod/assignment/locallib.php 28 Jun 2010 22:32:34 -0000 @@ -73,7 +73,7 @@ if (is_callable(array($this->assignment, 'portfolio_load_data'))) { return $this->assignment->portfolio_load_data($this); } - $this->set_file_and_format_data($this->fileid, $this->assignment->context->id, 'assignment_submission', $this->user->id, 'timemodified', false); + $this->set_file_and_format_data($this->fileid, $this->assignment->context->id, 'mod_assignment', 'submission', $this->user->id, 'timemodified', false); } public function prepare_package() { Index: backup/util/structure/simpletest/testbackupstructures.php =================================================================== RCS file: /cvsroot/moodle/moodle/backup/util/structure/simpletest/testbackupstructures.php,v retrieving revision 1.3 diff -u -r1.3 testbackupstructures.php --- backup/util/structure/simpletest/testbackupstructures.php 28 Jun 2010 20:54:21 -0000 1.3 +++ backup/util/structure/simpletest/testbackupstructures.php 28 Jun 2010 22:31:49 -0000 @@ -112,15 +112,15 @@ $p4id = $DB->insert_record('forum_posts', $post4); // With two related file $f1_post1 = (object)array( - 'contenthash' => 'testp1', 'contextid' => $this->contextid, - 'filearea' => 'forum_post', 'filename' => 'tp1', 'itemid' => $p1id, + 'contenthash' => 'testp1', 'contextid' => $this->contextid, 'component'=>'mod_forum', + 'filearea' => 'post', 'filename' => 'tp1', 'itemid' => $p1id, 'filesize' => 123, 'timecreated' => 0, 'timemodified' => 0, 'pathnamehash' => 'testp1' ); $DB->insert_record('files', $f1_post1); $f1_post2 = (object)array( - 'contenthash' => 'testp2', 'contextid' => $this->contextid, - 'filearea' => 'forum_attachment', 'filename' => 'tp2', 'itemid' => $p2id, + 'contenthash' => 'testp2', 'contextid' => $this->contextid, 'component'=>'mod_forum', + 'filearea' => 'attachment', 'filename' => 'tp2', 'itemid' => $p2id, 'filesize' => 123, 'timecreated' => 0, 'timemodified' => 0, 'pathnamehash' => 'testp2' ); @@ -293,10 +293,11 @@ $rating->set_source_alias('rating', 'post_rating'); // Map the 'rating' value from DB to 'post_rating' final element // Mark to detect files of type 'forum_intro' in forum (and not item id) - $forum->annotate_files(array('forum_intro'), null); + $forum->annotate_files('mod_forum', 'intro', null); // Mark to detect file of type 'forum_post' and 'forum_attachment' in post (with itemid being post->id) - $post->annotate_files(array('forum_post', 'forum_attachment'), 'id'); + $post->annotate_files('mod_forun', 'post', 'id'); + $post->annotate_files('mod_forum', 'attachment', 'id'); // Mark various elements to be annotated $discussion->annotate_ids('user1', 'userid'); @@ -569,30 +570,21 @@ } // Try various incorrect file annotations - $ne = new backup_nested_element('test', 'one', 'two', 'three'); - try { - $ne->annotate_files('notanarray', null); // Incorrect first param - $this->assertTrue(false, 'base_element_struct_exception expected'); - } catch (exception $e) { - $this->assertTrue($e instanceof base_element_struct_exception); - $this->assertEqual($e->errorcode, 'annotate_files_requires_array_of_areas'); - $this->assertEqual($e->a, 'notanarray'); - } $ne = new backup_nested_element('test', 'one', 'two', 'three'); - $ne->annotate_files(array('test_filearea'), null); + $ne->annotate_files('test', 'filearea', null); try { - $ne->annotate_files(array('test_filearea'), null); // Try to add annotations twice + $ne->annotate_files('test', 'filearea', null); // Try to add annotations twice $this->assertTrue(false, 'base_element_struct_exception expected'); } catch (exception $e) { $this->assertTrue($e instanceof base_element_struct_exception); - $this->assertEqual($e->errorcode, 'annotate_files_already_defined'); + $this->assertEqual($e->errorcode, 'annotate_files_duplicate_annotation'); $this->assertEqual($e->a, 'test'); } $ne = new backup_nested_element('test', 'one', 'two', 'three'); try { - $ne->annotate_files(array('test_filearea'), 'four'); // Incorrect element + $ne->annotate_files('test', 'filearea', 'four'); // Incorrect element $this->assertTrue(false, 'base_element_struct_exception expected'); } catch (exception $e) { $this->assertTrue($e instanceof base_element_struct_exception); Index: mod/url/backup/moodle2/backup_url_stepslib.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/url/backup/moodle2/backup_url_stepslib.php,v retrieving revision 1.1 diff -u -r1.1 backup_url_stepslib.php --- mod/url/backup/moodle2/backup_url_stepslib.php 13 May 2010 07:45:01 -0000 1.1 +++ mod/url/backup/moodle2/backup_url_stepslib.php 28 Jun 2010 22:33:00 -0000 @@ -21,7 +21,7 @@ * @copyright 2010 onwards Andrew Davis * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ - + /** * Define all the backup steps that will be used by the backup_url_activity_task */ @@ -32,7 +32,7 @@ class backup_url_activity_structure_step extends backup_activity_structure_step { protected function define_structure() { - + //the URL module stores no user info // Define each element separated @@ -51,7 +51,7 @@ //module has no id annotations // Define file annotations - $url->annotate_files(array('url_intro'), null); // This file area hasn't itemid + $url->annotate_files('mod_url', 'intro', null); // This file area hasn't itemid // Return the root element (url), wrapped into standard activity structure return $this->prepare_activity_structure($url); Index: repository/filepicker.js =================================================================== RCS file: /cvsroot/moodle/moodle/repository/filepicker.js,v retrieving revision 1.34 diff -u -r1.34 filepicker.js --- repository/filepicker.js 28 Jun 2010 02:13:58 -0000 1.34 +++ repository/filepicker.js 28 Jun 2010 22:33:08 -0000 @@ -99,7 +99,6 @@ params['accepted_types']=this.options.accepted_types; params['sesskey']=M.cfg.sesskey; params['client_id'] = args.client_id; - params['filearea'] = this.options.filearea?this.options.filearea:'user_draft'; params['itemid'] = this.options.itemid?this.options.itemid:0; params['maxbytes'] = this.options.maxbytes?this.options.maxbytes:-1; if (args['params']) { Index: repository/lib.php =================================================================== RCS file: /cvsroot/moodle/moodle/repository/lib.php,v retrieving revision 1.253 diff -u -r1.253 lib.php --- repository/lib.php 22 Jun 2010 07:40:30 -0000 1.253 +++ repository/lib.php 28 Jun 2010 22:33:10 -0000 @@ -830,6 +830,8 @@ $now = time(); $record->contextid = $context->id; + $record->component = 'user'; + $record->filearea = 'draft'; $record->timecreated = $now; $record->timemodified = $now; $record->userid = $USER->id; @@ -838,8 +840,7 @@ $record->itemid = 0; } $fs = get_file_storage(); - $browser = get_file_browser(); - if ($existingfile = $fs->get_file($context->id, $record->filearea, $record->itemid, $record->filepath, $record->filename)) { + if ($existingfile = $fs->get_file($context->id, $record->component, $record->filearea, $record->itemid, $record->filepath, $record->filename)) { $existingfile->delete(); } if ($file = $fs->create_file_from_pathname($record, $thefile)) { @@ -847,79 +848,18 @@ $delete = unlink($thefile); unset($CFG->repository_no_delete); } - $fileinfo = $browser->get_file_info($context, $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $file->get_filename()); - if(!empty($fileinfo)) { - return array( - 'url'=>$fileinfo->get_url(), - 'id'=>$file->get_itemid(), - 'file'=>$file->get_filename(), - 'icon' => $OUTPUT->pix_url(file_extension_icon($thefile, 32))->out() - ); - } else { - return null; - } + return array( + 'url'=>file_draftfile_url($file->get_itemid(), $file->get_filepath(), $file->get_filename()), + 'id'=>$file->get_itemid(), + 'file'=>$file->get_filename(), + 'icon' => $OUTPUT->pix_url(file_extension_icon($thefile, 32))->out(), + ); } else { return null; } } /** - * Return the user files tree in a format to be returned by the function get_listing - * @global object $CFG - * @param string $search - * @return array - */ - public static function get_user_file_tree($search = ''){ - global $CFG; - $ret = array(); - $ret['nologin'] = true; - $ret['manage'] = $CFG->wwwroot .'/files/index.php'; // temporary - $browser = get_file_browser(); - $itemid = null; - $filename = null; - $filearea = null; - $path = '/'; - $ret['dynload'] = false; - - if ($fileinfo = $browser->get_file_info(get_system_context(), $filearea, $itemid, $path, $filename)) { - - $ret['path'] = array(); - $params = $fileinfo->get_params(); - $filearea = $params['filearea']; - $ret['path'][] = repository::encode_path($filearea, $path, $fileinfo->get_visible_name()); - if ($fileinfo->is_directory()) { - $level = $fileinfo->get_parent(); - while ($level) { - $params = $level->get_params(); - $ret['path'][] = repository::encode_path($params['filearea'], $params['filepath'], $level->get_visible_name()); - $level = $level->get_parent(); - } - } - $filecount = repository::build_tree($fileinfo, $search, $ret['dynload'], $ret['list']); - $ret['path'] = array_reverse($ret['path']); - } - - if (empty($ret['list'])) { - //exit(mnet_server_fault(9016, get_string('emptyfilelist', 'repository_local'))); - throw new Exception('emptyfilelist'); - } else { - return $ret; - } - - } - - /** - * Serialize file path - * @param string $filearea - * @param string $path - * @param string $visiblename - * @return array - */ - public static function encode_path($filearea, $path, $visiblename) { - return array('path'=>serialize(array($filearea, $path)), 'name'=>$visiblename); - } - - /** * Builds a tree of files This function is * then called recursively. * @@ -950,7 +890,7 @@ $level = $child->get_parent(); while ($level) { $params = $level->get_params(); - $path[] = repository::encode_path($params['filearea'], $params['filepath'], $level->get_visible_name()); + $path[] = array($params['filepath'], $level->get_visible_name()); $level = $level->get_parent(); } @@ -989,7 +929,7 @@ continue; } $params = $child->get_params(); - $source = serialize(array($params['contextid'], $params['filearea'], $params['itemid'], $params['filepath'], $params['filename'])); + $source = serialize(array($params['contextid'], $params['component'], $params['filearea'], $params['itemid'], $params['filepath'], $params['filename'])); $list[] = array( 'title' => $filename, 'size' => $filesize, Index: repository/filepicker.php =================================================================== RCS file: /cvsroot/moodle/moodle/repository/filepicker.php,v retrieving revision 1.38 diff -u -r1.38 filepicker.php --- repository/filepicker.php 9 Jun 2010 06:44:31 -0000 1.38 +++ repository/filepicker.php 28 Jun 2010 22:33:09 -0000 @@ -32,6 +32,8 @@ /// Wait as long as it takes for this script to finish set_time_limit(0); +die('TODO: sorry, needs to be converted to use new component and security rules'); + require_login(); // disable blocks in this page @@ -49,7 +51,6 @@ $env = optional_param('env', 'filepicker', PARAM_ALPHA); // opened in file picker, file manager or html editor $filename = optional_param('filename', '', PARAM_FILE); $fileurl = optional_param('fileurl', '', PARAM_RAW); -$filearea = optional_param('filearea', 'user_draft', PARAM_TEXT); $thumbnail = optional_param('thumbnail', '', PARAM_RAW); $targetpath = optional_param('targetpath', '', PARAM_PATH); $repo_id = optional_param('repo_id', 0, PARAM_INT); // repository ID @@ -92,7 +93,7 @@ } } -$url = new moodle_url($CFG->httpswwwroot."/repository/filepicker.php", array('ctx_id' => $contextid, 'itemid' => $itemid, 'env' => $env, 'course'=>$courseid, 'filearea'=>$filearea)); +$url = new moodle_url($CFG->httpswwwroot."/repository/filepicker.php", array('ctx_id' => $contextid, 'itemid' => $itemid, 'env' => $env, 'course'=>$courseid)); $home_url = new moodle_url($url, array('action' => 'browse')); switch ($action) { @@ -106,7 +107,7 @@ case 'deletedraft': $contextid = $user_context->id; $fs = get_file_storage(); - if ($file = $fs->get_file($contextid, 'user_draft', $itemid, $draftpath, $filename)) { + if ($file = $fs->get_file($contextid, 'user', 'draft', $itemid, $draftpath, $filename)) { if ($file->is_directory()) { if ($file->get_parent_directory()) { $draftpath = $file->get_parent_directory()->get_filepath(); @@ -244,7 +245,8 @@ $record = new stdclass; $record->filepath = $draftpath; $record->filename = $filename; - $record->filearea = 'user_draft'; + $record->component = 'user'; + $record->filearea = 'draft'; $record->itemid = $itemid; $record->license = ''; $record->author = ''; @@ -261,7 +263,7 @@ $zipper = new zip_packer(); $fs = get_file_storage(); - $file = $fs->get_file($user_context->id, $filearea, $itemid, $draftpath, '.'); + $file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, '.'); if ($file->get_parent_directory()) { $parent_path = $file->get_parent_directory()->get_filepath(); $filename = trim($draftpath, '/').'.zip'; @@ -334,14 +336,14 @@ $zipper = new zip_packer(); $fs = get_file_storage(); - $file = $fs->get_file($user_context->id, $filearea, $itemid, $draftpath, '.'); + $file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, '.'); if (!$file->get_parent_directory()) { $parent_path = '/'; } else { $parent_path = $file->get_parent_directory()->get_filepath(); } - $newfile = $zipper->archive_to_storage(array($file), $user_context->id, $filearea, $itemid, $parent_path, $file->get_filepath().'.zip', $USER->id); + $newfile = $zipper->archive_to_storage(array($file), $user_context->id, 'user', 'draft', $itemid, $parent_path, $file->get_filepath().'.zip', $USER->id); $url->param('action', 'browse'); $url->param('draftpath', $parent_path); @@ -351,9 +353,9 @@ case 'unzip': $zipper = new zip_packer(); $fs = get_file_storage(); - $file = $fs->get_file($user_context->id, $filearea, $itemid, $draftpath, $filename); + $file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, $filename); - if ($newfile = $file->extract_to_storage($zipper, $user_context->id, $filearea, $itemid, $draftpath, $USER->id)) { + if ($newfile = $file->extract_to_storage($zipper, $user_context->id, 'user', 'draft', $itemid, $draftpath, $USER->id)) { $str = get_string('unziped','repository'); } else { $str = get_string('cannotunzip', 'repository'); @@ -366,8 +368,8 @@ case 'movefile': if (!empty($targetpath)) { $fb = get_file_browser(); - $file = $fb->get_file_info($user_context, $filearea, $itemid, $draftpath, $filename); - $file->copy_to_storage($user_context->id, $filearea, $itemid, $targetpath, $filename); + $file = $fb->get_file_info($user_context, 'user', 'draft', $itemid, $draftpath, $filename); + $file->copy_to_storage($user_context->id, 'user', 'draft', $itemid, $targetpath, $filename); if ($file->delete()) { $url->param('action', 'browse'); $url->param('draftpath', $targetpath); @@ -381,7 +383,7 @@ $url->param('action', 'movefile'); $url->param('draftpath', $draftpath); $url->param('filename', $filename); - file_get_user_area_folders($itemid, '/', $data); + file_get_drafarea_folders($itemid, '/', $data); print_draft_area_tree($data, true, $url); echo $OUTPUT->footer(); break; @@ -400,7 +402,7 @@ case 'mkdir': $fs = get_file_storage(); - $fs->create_directory($user_context->id, $filearea, $itemid, file_correct_filepath(file_correct_filepath($draftpath).trim($newdirname, '/'))); + $fs->create_directory($user_context->id, 'user', 'draft', $itemid, file_correct_filepath(file_correct_filepath($draftpath).trim($newdirname, '/'))); $url->param('action', 'browse'); $url->param('draftpath', $draftpath); if (!empty($newdirname)) { @@ -413,7 +415,7 @@ case 'rename': $fs = get_file_storage(); - if ($file = $fs->get_file($user_context->id, $filearea, $itemid, $draftpath, $filename)) { + if ($file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, $filename)) { if ($file->is_directory()) { if ($file->get_parent_directory()) { $draftpath = $file->get_parent_directory()->get_filepath(); @@ -422,12 +424,12 @@ } // use file storage to create new folder $newdir = $draftpath . trim($newfilename , '/') . '/'; - $fs->create_directory($user_context->id, $filearea, $itemid, $newdir); + $fs->create_directory($user_context->id, 'user', 'draft', $itemid, $newdir); } else { // use file browser to copy file $fb = get_file_browser(); - $file = $fb->get_file_info($user_context, $filearea, $itemid, $draftpath, $filename); - $file->copy_to_storage($user_context->id, $filearea, $itemid, $draftpath, $newfilename); + $file = $fb->get_file_info($user_context, 'user', 'draft', $itemid, $draftpath, $filename); + $file->copy_to_storage($user_context->id, 'user', 'draft', $itemid, $draftpath, $newfilename); } } $file->delete(); @@ -459,7 +461,7 @@ $params['returntypes'] = 2; $repos = repository::get_instances($params); $fs = get_file_storage(); - $files = $fs->get_directory_files($user_context->id, $filearea, $itemid, $draftpath, false); + $files = $fs->get_directory_files($user_context->id, 'user', 'draft', $itemid, $draftpath, false); echo $OUTPUT->header(); if ((!empty($files) or $draftpath != '/') and $env == 'filemanager') { @@ -501,7 +503,7 @@ if (!empty($files)) { echo '
      '; foreach ($files as $file) { - $drafturl = new moodle_url($CFG->httpswwwroot.'/draftfile.php/'.$user_context->id.'/'.$filearea.'/'.$itemid.'/'.$file->get_filename()); + $drafturl = new moodle_url($CFG->httpswwwroot.'/draftfile.php/'.$user_context->id.'/user/draft/'.$itemid.'/'.$file->get_filename()); if ($file->get_filename() != '.') { // a file $fileicon = $CFG->wwwroot.'/pix/'.(file_extension_icon($file->get_filename())); Index: repository/repository_ajax.php =================================================================== RCS file: /cvsroot/moodle/moodle/repository/repository_ajax.php,v retrieving revision 1.28 diff -u -r1.28 repository_ajax.php --- repository/repository_ajax.php 28 Jun 2010 02:13:58 -0000 1.28 +++ repository/repository_ajax.php 28 Jun 2010 22:33:10 -0000 @@ -35,7 +35,6 @@ /// Parameters $action = optional_param('action', '', PARAM_ALPHA); $repo_id = optional_param('repo_id', 0, PARAM_INT); // Pepository ID -$callback = optional_param('callback', '', PARAM_CLEANHTML); // Is this a callback from external site? $client_id = optional_param('client_id', '', PARAM_RAW); // Client ID $contextid = optional_param('ctx_id', SYSCONTEXTID, PARAM_INT); // Context ID $env = optional_param('env', 'filepicker', PARAM_ALPHA); // Opened in editor or moodleform @@ -46,7 +45,6 @@ $page = optional_param('page', '', PARAM_RAW); // Page $maxbytes = optional_param('maxbytes', 0, PARAM_INT); // Maxbytes $req_path = optional_param('p', '', PARAM_RAW); // Path -$saveas_filearea = optional_param('filearea', 'user_draft', PARAM_TEXT); $saveas_filename = optional_param('title', '', PARAM_FILE); // save as file name $saveas_path = optional_param('savepath', '/', PARAM_PATH); // save as file path $search_text = optional_param('s', '', PARAM_CLEANHTML); @@ -140,41 +138,6 @@ die(json_encode($err)); } - -if (!empty($callback)) { - // post callback - $repo->callback(); - // call opener window to refresh repository - // the callback url should be something like this: - // http://xx.moodle.com/repository/repository_ajax.php?callback=yes&repo_id=1&sid=xxx - // sid is the attached auth token from external source - // If Moodle is working on HTTPS mode, then we are not allowed to access - // parent window, in this case, we need to alert user to refresh the repository - // manually. - $strhttpsbug = get_string('cannotaccessparentwin', 'repository'); - $strrefreshnonjs = get_string('refreshnonjsfilepicker', 'repository'); - $js =<< - - - - - - - -EOD; - die($js); -} - /// These actions all occur on the currently active repository instance switch ($action) { case 'sign': @@ -235,7 +198,7 @@ // so we don't check user quota and maxbytes here if (in_array($repo->options['type'], array('local', 'recent', 'user'))) { try { - $fileinfo = $repo->copy_to_area($source, $saveas_filearea, $itemid, $saveas_path, $saveas_filename); + $fileinfo = $repo->copy_to_area($source, 'draft', $itemid, $saveas_path, $saveas_filename); } catch (Exception $e) { throw $e; } @@ -296,7 +259,8 @@ $record = new stdclass; $record->filepath = $saveas_path; $record->filename = $saveas_filename; - $record->filearea = $saveas_filearea; + $record->component = 'user'; + $record->filearea = 'draft'; $record->itemid = $itemid; if (!empty($file['license'])) { Index: mod/imscp/backup/moodle2/backup_imscp_stepslib.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/imscp/backup/moodle2/backup_imscp_stepslib.php,v retrieving revision 1.1 diff -u -r1.1 backup_imscp_stepslib.php --- mod/imscp/backup/moodle2/backup_imscp_stepslib.php 4 May 2010 18:28:31 -0000 1.1 +++ mod/imscp/backup/moodle2/backup_imscp_stepslib.php 28 Jun 2010 22:32:51 -0000 @@ -51,7 +51,7 @@ // (none) // Define file annotations - $imscp->annotate_files(array('imscp_intro'), null); // This file area hasn't itemid + $imscp->annotate_files('mod_imscp', 'intro', null); // This file area hasn't itemid /** * Don't annotate contents for now. It breaks backup as far as it's using itemid for storing * revisions. Each element only can have one files anotation and itemid must be null or id for all them @@ -62,7 +62,7 @@ * * TODO: To decide MDL-22315 comments about this. */ - // $imscp->annotate_files(array('imscp_content'), 'revision'); // This file area uses 'revision' as itemid + $imscp->annotate_files('mod_imscp', 'content', null); // TODO: backup everything as is for now, the revision would be the same after restore // Return the root element (imscp), wrapped into standard activity structure return $this->prepare_activity_structure($imscp); Index: mod/workshop/exsubmission.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/workshop/exsubmission.php,v retrieving revision 1.9 diff -u -r1.9 exsubmission.php --- mod/workshop/exsubmission.php 8 Jun 2010 23:10:06 -0000 1.9 +++ mod/workshop/exsubmission.php 28 Jun 2010 22:33:04 -0000 @@ -102,9 +102,9 @@ $contentopts = array('trusttext' => true, 'subdirs' => false, 'maxfiles' => $maxfiles, 'maxbytes' => $maxbytes); $attachmentopts = array('subdirs' => true, 'maxfiles' => $maxfiles, 'maxbytes' => $maxbytes); $example = file_prepare_standard_editor($example, 'content', $contentopts, $workshop->context, - 'workshop_submission_content', $example->id); + 'mod_workshop', 'submission_content', $example->id); $example = file_prepare_standard_filemanager($example, 'attachment', $attachmentopts, $workshop->context, - 'workshop_submission_attachment', $example->id); + 'mod_workshop', 'submission_attachment', $example->id); $mform = new workshop_submission_form($PAGE->url, array('current' => $example, 'workshop' => $workshop, 'contentopts' => $contentopts, 'attachmentopts' => $attachmentopts)); @@ -132,9 +132,9 @@ } // save and relink embedded images and save attachments $formdata = file_postupdate_standard_editor($formdata, 'content', $contentopts, $workshop->context, - 'workshop_submission_content', $formdata->id); + 'mod_workshop', 'submission_content', $formdata->id); $formdata = file_postupdate_standard_filemanager($formdata, 'attachment', $attachmentopts, $workshop->context, - 'workshop_submission_attachment', $formdata->id); + 'mod_workshop', 'submission_attachment', $formdata->id); if (empty($formdata->attachment)) { // explicit cast to zero integer $formdata->attachment = 0; Index: mod/workshop/submission.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/workshop/submission.php,v retrieving revision 1.41 diff -u -r1.41 submission.php --- mod/workshop/submission.php 14 Jun 2010 10:28:36 -0000 1.41 +++ mod/workshop/submission.php 28 Jun 2010 22:33:05 -0000 @@ -102,9 +102,9 @@ $contentopts = array('trusttext' => true, 'subdirs' => false, 'maxfiles' => $maxfiles, 'maxbytes' => $maxbytes); $attachmentopts = array('subdirs' => true, 'maxfiles' => $maxfiles, 'maxbytes' => $maxbytes); $submission = file_prepare_standard_editor($submission, 'content', $contentopts, $workshop->context, - 'workshop_submission_content', $submission->id); + 'mod_workshop', 'submission_content', $submission->id); $submission = file_prepare_standard_filemanager($submission, 'attachment', $attachmentopts, $workshop->context, - 'workshop_submission_attachment', $submission->id); + 'mod_workshop', 'submission_attachment', $submission->id); $mform = new workshop_submission_form($PAGE->url, array('current' => $submission, 'workshop' => $workshop, 'contentopts' => $contentopts, 'attachmentopts' => $attachmentopts)); @@ -132,9 +132,9 @@ } // save and relink embedded images and save attachments $formdata = file_postupdate_standard_editor($formdata, 'content', $contentopts, $workshop->context, - 'workshop_submission_content', $formdata->id); + 'mod_workshop', 'submission_content', $formdata->id); $formdata = file_postupdate_standard_filemanager($formdata, 'attachment', $attachmentopts, $workshop->context, - 'workshop_submission_attachment', $formdata->id); + 'mod_workshop', 'submission_attachment', $formdata->id); if (empty($formdata->attachment)) { // explicit cast to zero integer $formdata->attachment = 0; Index: mod/workshop/mod_form.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/workshop/mod_form.php,v retrieving revision 1.16 diff -u -r1.16 mod_form.php --- mod/workshop/mod_form.php 2 May 2010 21:50:10 -0000 1.16 +++ mod/workshop/mod_form.php 28 Jun 2010 22:33:04 -0000 @@ -192,7 +192,7 @@ // editing an existing workshop - let us prepare the added editor elements (intro done automatically) $draftitemid = file_get_submitted_draft_itemid('instructauthors'); $data['instructauthorseditor']['text'] = file_prepare_draft_area($draftitemid, $this->context->id, - 'workshop_instructauthors', false, + 'mod_workshop', 'instructauthors', false, workshop::instruction_editors_options($this->context), $data['instructauthors']); $data['instructauthorseditor']['format'] = $data['instructauthorsformat']; @@ -200,7 +200,7 @@ $draftitemid = file_get_submitted_draft_itemid('instructreviewers'); $data['instructreviewerseditor']['text'] = file_prepare_draft_area($draftitemid, $this->context->id, - 'workshop_instructreviewers', false, + 'mod_workshop', 'instructreviewers', false, workshop::instruction_editors_options($this->context), $data['instructreviewers']); $data['instructreviewerseditor']['format'] = $data['instructreviewersformat']; Index: mod/workshop/view.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/workshop/view.php,v retrieving revision 1.141 diff -u -r1.141 view.php --- mod/workshop/view.php 14 Jun 2010 14:48:56 -0000 1.141 +++ mod/workshop/view.php 28 Jun 2010 22:33:05 -0000 @@ -96,7 +96,7 @@ case workshop::PHASE_SUBMISSION: if (trim(strip_tags($workshop->instructauthors))) { $instructions = file_rewrite_pluginfile_urls($workshop->instructauthors, 'pluginfile.php', $PAGE->context->id, - 'workshop_instructauthors', 0, workshop::instruction_editors_options($PAGE->context)); + 'mod_workshop', 'instructauthors', 0, workshop::instruction_editors_options($PAGE->context)); print_collapsible_region_start('', 'workshop-viewlet-instructauthors', get_string('instructauthors', 'workshop')); echo $output->box(format_text($instructions, $workshop->instructauthorsformat), array('generalbox', 'instructions')); print_collapsible_region_end(); @@ -201,7 +201,7 @@ } if (trim(strip_tags($workshop->instructreviewers))) { $instructions = file_rewrite_pluginfile_urls($workshop->instructreviewers, 'pluginfile.php', $PAGE->context->id, - 'workshop_instructreviewers', 0, workshop::instruction_editors_options($PAGE->context)); + 'mod_workshop', 'instructreviewers', 0, workshop::instruction_editors_options($PAGE->context)); print_collapsible_region_start('', 'workshop-viewlet-instructreviewers', get_string('instructreviewers', 'workshop')); echo $output->box(format_text($instructions, $workshop->instructreviewersformat), array('generalbox', 'instructions')); print_collapsible_region_end(); Index: mod/workshop/renderer.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/workshop/renderer.php,v retrieving revision 1.57 diff -u -r1.57 renderer.php --- mod/workshop/renderer.php 14 Jun 2010 10:28:36 -0000 1.57 +++ mod/workshop/renderer.php 28 Jun 2010 22:33:05 -0000 @@ -202,7 +202,7 @@ $content = format_text($submission->content, $submission->contentformat); $content = file_rewrite_pluginfile_urls($content, 'pluginfile.php', $this->page->context->id, - 'workshop_submission_content', $submission->id); + 'workshop_submission', 'content', $submission->id); $o .= $this->output->container($content, 'content'); $o .= $this->submission_attachments($submission); @@ -228,7 +228,7 @@ $fs = get_file_storage(); $ctx = $this->page->context; - $files = $fs->get_area_files($ctx->id, 'workshop_submission_attachment', $submission->id); + $files = $fs->get_area_files($ctx->id, 'mod_workshop', 'submission_attachment', $submission->id); $outputimgs = ""; // images to be displayed inline $outputfiles = ""; // list of attachment files @@ -241,7 +241,7 @@ $filepath = $file->get_filepath(); $filename = $file->get_filename(); $fileurl = file_encode_url($CFG->wwwroot . '/pluginfile.php', - '/' . $ctx->id . '/workshop_submission_attachment/' . $submission->id . $filepath . $filename, true); + '/' . $ctx->id . '/mod_workshop/submission_attachment/' . $submission->id . $filepath . $filename, true); $type = $file->get_mimetype(); $type = mimeinfo_from_type("type", $type); $image = html_writer::empty_tag('img', array('src'=>$this->output->pix_url(file_mimetype_icon($type)), 'alt'=>$type, 'class'=>'icon')); @@ -349,7 +349,7 @@ $content = format_text($example->content, $example->contentformat); $content = file_rewrite_pluginfile_urls($content, 'pluginfile.php', $this->page->context->id, - 'workshop_submission_content', $example->id); + 'workshop_submission', 'content', $example->id); $o .= $this->output->container($content, 'content'); $o .= $this->submission_attachments($example); Index: mod/workshop/fileinfolib.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/workshop/fileinfolib.php,v retrieving revision 1.3 diff -u -r1.3 fileinfolib.php --- mod/workshop/fileinfolib.php 4 Jan 2010 17:49:01 -0000 1.3 +++ mod/workshop/fileinfolib.php 28 Jun 2010 22:33:04 -0000 @@ -54,6 +54,7 @@ */ public function get_params() { return array('contextid'=>$this->context->id, + 'component'=>'mod_workshop', 'filearea' =>$this->filearea, 'itemid' =>null, 'filepath' =>null, @@ -92,7 +93,7 @@ global $DB; $children = array(); - $itemids = $DB->get_records('files', array('contextid' => $this->context->id, 'filearea' => $this->filearea), + $itemids = $DB->get_records('files', array('contextid' => $this->context->id, 'component' => 'mod_workshop', 'filearea' => $this->filearea), 'itemid', "DISTINCT itemid"); foreach ($itemids as $itemid => $unused) { if ($child = $this->browser->get_file_info($this->context, $this->filearea, $itemid)) { Index: mod/workshop/lib.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/workshop/lib.php,v retrieving revision 1.149 diff -u -r1.149 lib.php --- mod/workshop/lib.php 5 Jun 2010 19:53:18 -0000 1.149 +++ mod/workshop/lib.php 28 Jun 2010 22:33:04 -0000 @@ -83,13 +83,13 @@ // process the custom wysiwyg editors if ($draftitemid = $workshop->instructauthorseditor['itemid']) { - $workshop->instructauthors = file_save_draft_area_files($draftitemid, $context->id, 'workshop_instructauthors', + $workshop->instructauthors = file_save_draft_area_files($draftitemid, $context->id, 'mod_workshop', 'instructauthors', 0, workshop::instruction_editors_options($context), $workshop->instructauthorseditor['text']); $workshop->instructauthorsformat = $workshop->instructauthorseditor['format']; } if ($draftitemid = $workshop->instructreviewerseditor['itemid']) { - $workshop->instructreviewers = file_save_draft_area_files($draftitemid, $context->id, 'workshop_instructreviewers', + $workshop->instructreviewers = file_save_draft_area_files($draftitemid, $context->id, 'mod_workshop', 'instructreviewers', 0, workshop::instruction_editors_options($context), $workshop->instructreviewerseditor['text']); $workshop->instructreviewersformat = $workshop->instructreviewerseditor['format']; } @@ -130,13 +130,13 @@ // process the custom wysiwyg editors if ($draftitemid = $workshop->instructauthorseditor['itemid']) { - $workshop->instructauthors = file_save_draft_area_files($draftitemid, $context->id, 'workshop_instructauthors', + $workshop->instructauthors = file_save_draft_area_files($draftitemid, $context->id, 'mod_workshop', 'instructauthors', 0, workshop::instruction_editors_options($context), $workshop->instructauthorseditor['text']); $workshop->instructauthorsformat = $workshop->instructauthorseditor['format']; } if ($draftitemid = $workshop->instructreviewerseditor['itemid']) { - $workshop->instructreviewers = file_save_draft_area_files($draftitemid, $context->id, 'workshop_instructreviewers', + $workshop->instructreviewers = file_save_draft_area_files($draftitemid, $context->id, 'mod_workshop', 'instructreviewers', 0, workshop::instruction_editors_options($context), $workshop->instructreviewerseditor['text']); $workshop->instructreviewersformat = $workshop->instructreviewerseditor['format']; } @@ -428,7 +428,7 @@ * Returns the lists of all browsable file areas within the given module context * * The file area workshop_intro for the activity introduction field is added automatically - * by {@link file_browser::get_file_info_module()} + * by {@link file_browser::get_file_info_context_module()} * * @param stdclass $course * @param stdclass $cm @@ -437,12 +437,11 @@ */ function workshop_get_file_areas($course, $cm, $context) { $areas = array(); - if (has_capability('moodle/course:managefiles', $context)) { - $areas['workshop_instructauthors'] = get_string('areainstructauthors', 'workshop'); - $areas['workshop_instructreviewers'] = get_string('areainstructreviewers', 'workshop'); - $areas['workshop_submission_content'] = get_string('areasubmissioncontent', 'workshop'); - $areas['workshop_submission_attachment'] = get_string('areasubmissionattachment', 'workshop'); - } + $areas['instructauthors'] = get_string('areainstructauthors', 'workshop'); + $areas['instructreviewers'] = get_string('areainstructreviewers', 'workshop'); + $areas['submission_content'] = get_string('areasubmissioncontent', 'workshop'); + $areas['submission_attachment'] = get_string('areasubmissionattachment', 'workshop'); + return $areas; } @@ -454,37 +453,35 @@ * the fileareas workshop_submission_content and workshop_submission_attachment are used. * The access rights to the files are checked here. The user must be either a peer-reviewer * of the submission or have capability ... (todo) to access the submission files. - * Besides that, areas workshop_instructauthors and workshop_instructreviewers contain the media + * Besides that, areas workshop_instructauthors and mod_workshop instructreviewers contain the media * embedded using the mod_form.php. * * @param stdclass $course - * @param stdclass $cminfo + * @param stdclass $cm * @param stdclass $context * @param string $filearea * @param array $args * @param bool $forcedownload * @return void this should never return to the caller */ -function workshop_pluginfile($course, $cminfo, $context, $filearea, array $args, $forcedownload) { +function workshop_pluginfile($course, $cm, $context, $filearea, array $args, $forcedownload) { global $DB; - if (!$cminfo->uservisible) { - send_file_not_found(); - } - if (!$cm = get_coursemodule_from_instance('workshop', $cminfo->instance, $course->id)) { - send_file_not_found(); + if ($context->contextlevel != CONTEXT_MODULE) { + return false; } + require_login($course, true, $cm); - if ($filearea === 'workshop_instructauthors') { + if ($filearea === 'instructauthors') { // submission instructions may contain sensitive data if (!has_any_capability(array('moodle/course:manageactivities', 'mod/workshop:submit'), $context)) { send_file_not_found(); } array_shift($args); // we do not use itemids here - $relativepath = '/' . implode('/', $args); - $fullpath = $context->id . $filearea . '0' . $relativepath; // beware, slashes are not used here! + $relativepath = implode('/', $args); + $fullpath = "/$context->id/mod_workshop/$filearea/0/$relativepath"; // beware, slashes are not used here! $fs = get_file_storage(); if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) { @@ -497,15 +494,15 @@ send_stored_file($file, $lifetime, 0); } - if ($filearea === 'workshop_instructreviewers') { + if ($filearea === 'instructreviewers') { // submission instructions may contain sensitive data if (!has_any_capability(array('moodle/course:manageactivities', 'mod/workshop:peerassess'), $context)) { send_file_not_found(); } array_shift($args); // we do not use itemids here - $relativepath = '/' . implode('/', $args); - $fullpath = $context->id . $filearea . '0' . $relativepath; // beware, slashes are not used here! + $relativepath = implode('/', $args); + $fullpath = "/$context->id/mod_workshop/$filearea/0/$relativepath"; // beware, slashes are not used here! $fs = get_file_storage(); if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) { @@ -516,67 +513,19 @@ // finally send the file send_stored_file($file, $lifetime, 0); - } - - // the following file areas are for the files embedded into the assessment forms - // TODO this should be rewritten to using callbacks into subplugins - if (in_array($filearea, array( - 'workshopform_comments_description', - 'workshopform_accumulative_description', - 'workshopform_numerrors_description', - 'workshopform_rubric_description', - ))) { - $itemid = (int)array_shift($args); // the id of the assessment form dimension - if (!$workshop = $DB->get_record('workshop', array('id' => $cminfo->instance))) { - send_file_not_found(); - } - switch ($filearea) { - case 'workshopform_comments_description': - $dimension = $DB->get_record('workshopform_comments', array('id' => $itemid)); - break; - case 'workshopform_accumulative_description': - $dimension = $DB->get_record('workshopform_accumulative', array('id' => $itemid)); - break; - case 'workshopform_numerrors_description': - $dimension = $DB->get_record('workshopform_numerrors', array('id' => $itemid)); - break; - case 'workshopform_rubric_description': - $dimension = $DB->get_record('workshopform_rubric', array('id' => $itemid)); - break; - default: - $dimension = false; - } - if (empty($dimension)) { - send_file_not_found(); - } - if ($workshop->id != $dimension->workshopid) { - // this should never happen but just in case - send_file_not_found(); - } - // TODO now make sure the user is allowed to see the file - // (media embedded into the dimension description) - $fs = get_file_storage(); - $relativepath = '/' . implode('/', $args); - $fullpath = $context->id . $filearea . $itemid . $relativepath; - if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) { - return false; - } - // finally send the file - send_stored_file($file); - } - if ($filearea == 'workshop_submission_content' or $filearea == 'workshop_submission_attachment') { + } else if ($filearea === 'submission_content' or $filearea === 'submission_attachment') { $itemid = (int)array_shift($args); - if (!$submission = $DB->get_record('workshop_submissions', array('id' => $itemid))) { + if (!$workshop = $DB->get_record('workshop', array('id' => $cm->instance))) { return false; } - if (!$workshop = $DB->get_record('workshop', array('id' => $cminfo->instance))) { + if (!$submission = $DB->get_record('workshop_submissions', array('id' => $itemid, 'workshopid' => $workshop->id))) { return false; } // TODO now make sure the user is allowed to see the file $fs = get_file_storage(); - $relativepath = '/' . implode('/', $args); - $fullpath = $context->id . $filearea . $itemid . $relativepath; + $relativepath = implode('/', $args); + $fullpath = "/$context->id/mod_workshop/$filearea/$itemid/$relativepath"; if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) { return false; } @@ -611,7 +560,7 @@ $fs = get_file_storage(); - if ($filearea === 'workshop_submission_content' or $filearea === 'workshop_submission_attachment') { + if ($filearea === 'content' or $filearea === 'attachment') { if (is_null($itemid)) { require_once($CFG->dirroot . '/mod/workshop/fileinfolib.php'); @@ -623,9 +572,9 @@ $filepath = is_null($filepath) ? '/' : $filepath; $filename = is_null($filename) ? '.' : $filename; - if (!$storedfile = $fs->get_file($context->id, $filearea, $itemid, $filepath, $filename)) { + if (!$storedfile = $fs->get_file($context->id, 'mod_workshop', $filearea, $itemid, $filepath, $filename)) { if ($filepath === '/' and $filename === '.') { - $storedfile = new virtual_root_file($context->id, $filearea, $itemid); + $storedfile = new virtual_root_file($context->id, 'mod_workshop', $filearea, $itemid); } else { // not found return null; @@ -647,16 +596,16 @@ return new file_info_stored($browser, $context, $storedfile, $urlbase, $topvisiblename, true, true, false, false); } - if ($filearea == 'workshop_instructauthors' or $filearea == 'workshop_instructreviewers') { + if ($filearea == 'instructauthors' or $filearea == 'instructreviewers') { // always only itemid 0 $filepath = is_null($filepath) ? '/' : $filepath; $filename = is_null($filename) ? '.' : $filename; $urlbase = $CFG->wwwroot.'/pluginfile.php'; - if (!$storedfile = $fs->get_file($context->id, $filearea, 0, $filepath, $filename)) { + if (!$storedfile = $fs->get_file($context->id, 'mod_workshop', $filearea, 0, $filepath, $filename)) { if ($filepath === '/' and $filename === '.') { - $storedfile = new virtual_root_file($context->id, $filearea, 0); + $storedfile = new virtual_root_file($context->id, 'mod_workshop', $filearea, 0); } else { // not found return null; Index: mod/data/field/picture/field.class.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/data/field/picture/field.class.php,v retrieving revision 1.37 diff -u -r1.37 field.class.php --- mod/data/field/picture/field.class.php 24 May 2010 09:05:40 -0000 1.37 +++ mod/data/field/picture/field.class.php 28 Jun 2010 22:32:39 -0000 @@ -39,20 +39,19 @@ if ($recordid) { if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) { - file_prepare_draft_area($itemid, $this->context->id, 'data_content', $content->id); + file_prepare_draft_area($itemid, $this->context->id, 'mod_data', 'content', $content->id); if (!empty($content->content)) { - if ($file = $fs->get_file($this->context->id, 'data_content', $content->id, '/', $content->content)) { + if ($file = $fs->get_file($this->context->id, 'mod_data', 'content', $content->id, '/', $content->content)) { $usercontext = get_context_instance(CONTEXT_USER, $USER->id); - if (!$files = $fs->get_area_files($usercontext->id, 'user_draft', $itemid, 'id DESC', false)) { + if (!$files = $fs->get_area_files($usercontext->id, 'user', 'draft', $itemid, 'id DESC', false)) { return false; } - if ($thumbfile = $fs->get_file($usercontext->id, 'user_draft', $itemid, '/', 'thumb_'.$content->content)) { + if ($thumbfile = $fs->get_file($usercontext->id, 'user', 'draft', $itemid, '/', 'thumb_'.$content->content)) { $thumbfile->delete(); } if (empty($content->content1)) { // Print icon if file already exists - $browser = get_file_browser(); - $src = file_encode_url($CFG->wwwroot.'/draftfile.php/', $usercontext->id.'/user_draft/'.$itemid.'/'.$file->get_filename()); + $src = file_draftfile_url($itemid, '/', $file->get_filename()); $displayname = ''.$file->get_mimetype().''. ''.s($file->get_filename()).''; } else { @@ -69,7 +68,7 @@ $str = '
      '; $str .= '
      '.$this->field->name.''; if ($file) { - $src = file_encode_url($CFG->wwwroot.'/pluginfile.php/', $this->context->id.'/data_content/'.$content->id.'/'.$file->get_filename()); + $src = file_encode_url($CFG->wwwroot.'/pluginfile.php/', $this->context->id.'/mod_data/content/'.$content->id.'/'.$file->get_filename()); $str .= ''; } @@ -111,7 +110,7 @@ } } $fs = get_file_storage(); - if (!$file = $fs->get_file($this->context->id, 'data_content', $content->id, '/', $content->content)) { + if (!$file = $fs->get_file($this->context->id, 'mod_data', 'content', $content->id, '/', $content->content)) { return null; } @@ -148,18 +147,16 @@ return ''; } - $browser = get_file_browser(); - $alt = $content->content1; $title = $alt; if ($template == 'listtemplate') { - $src = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/data_content/'.$content->id.'/'.'thumb_'.$content->content); + $src = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/mod_data/content/'.$content->id.'/'.'thumb_'.$content->content); // no need to add width/height, because the thumb is resized properly $str = ''.s($alt).''; } else { - $src = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/data_content/'.$content->id.'/'.$content->content); + $src = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/mod_data/content/'.$content->id.'/'.$content->content); $width = $this->field->param1 ? ' width="'.s($this->field->param1).'" ':' '; $height = $this->field->param2 ? ' height="'.s($this->field->param2).'" ':' '; $str = ''.s($alt).''; @@ -187,10 +184,10 @@ ob_flush(); } foreach ($contents as $content) { - if (!$file = $fs->get_file($this->context->id, 'data_content', $content->id, '/', $content->content)) { + if (!$file = $fs->get_file($this->context->id, 'mod_data', 'content', $content->id, '/', $content->content)) { continue; } - if ($thumbfile = $fs->get_file($this->context->id, 'data_content', $content->id, '/', 'thumb_'.$content->content)) { + if ($thumbfile = $fs->get_file($this->context->id, 'mod_data', 'content', $content->id, '/', 'thumb_'.$content->content)) { $thumbfile->delete(); } @set_time_limit(300); @@ -218,15 +215,15 @@ switch ($names[2]) { case 'file': $fs = get_file_storage(); - $fs->delete_area_files($this->context->id, 'data_content', $content->id); + $fs->delete_area_files($this->context->id, 'mod_data', 'content', $content->id); $usercontext = get_context_instance(CONTEXT_USER, $USER->id); - $files = $fs->get_area_files($usercontext->id, 'user_draft', $value); + $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $value); if (count($files)<2) { // no file } else { $count = 0; foreach ($files as $draftfile) { - $file_record = array('contextid'=>$this->context->id, 'itemid'=>$content->id, 'filepath'=>'/', 'filearea'=>'data_content'); + $file_record = array('contextid'=>$this->context->id, 'component'=>'mod_data', 'filearea'=>'content', 'itemid'=>$content->id, 'filepath'=>'/'); if (!$draftfile->is_directory()) { $file_record['filename'] = $draftfile->get_filename(); @@ -263,7 +260,7 @@ // If thumbnail width and height are BOTH not specified then no thumbnail is generated, and // additionally an attempted delete of the existing thumbnail takes place. $fs = get_file_storage(); - $file_record = array('contextid'=>$file->get_contextid(), 'filearea'=>$file->get_filearea(), + $file_record = array('contextid'=>$file->get_contextid(), 'component'=>$file->get_component(), 'filearea'=>$file->get_filearea(), 'itemid'=>$file->get_itemid(), 'filepath'=>$file->get_filepath(), 'filename'=>'thumb_'.$file->get_filename(), 'userid'=>$file->get_userid()); try { Index: mod/resource/db/upgradelib.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/resource/db/upgradelib.php,v retrieving revision 1.2 diff -u -r1.2 upgradelib.php --- mod/resource/db/upgradelib.php 22 May 2010 13:54:42 -0000 1.2 +++ mod/resource/db/upgradelib.php 28 Jun 2010 22:32:56 -0000 @@ -67,8 +67,8 @@ $context = get_context_instance(CONTEXT_MODULE, $candidate->cmid); $sitecontext = get_context_instance(CONTEXT_COURSE, $siteid); - $file_record = array('contextid'=>$context->id, 'filearea'=>'resource_content', 'itemid'=>0); - if ($file = $fs->get_file_by_hash(sha1($sitecontext->id.'course_content0'.$path))) { + $file_record = array('contextid'=>$context->id, 'component'=>'mod_resourse', 'filearea'=>'content', 'itemid'=>0); + if ($file = $fs->get_file_by_hash(sha1("/$sitecontext->id/course/legacy/content/0".$path))) { try { $fs->create_file_from_storedfile($file_record, $file); } catch (Exception $x) { @@ -93,7 +93,7 @@ } // try migration of main file - ignore if does not exist - if ($file = resourcelib_try_file_migration($resource->mainfile, $candidate->cmid, $candidate->course, 'resource_content', 0)) { + if ($file = resourcelib_try_file_migration($resource->mainfile, $candidate->cmid, $candidate->course, 'mod_resource', 'content', 0)) { $resource->mainfile = $file->get_filepath().$file->get_filename(); } @@ -127,7 +127,7 @@ } // try migration of main file - ignore if does not exist - if ($file = resourcelib_try_file_migration($resource->mainfile, $candidate->cmid, $candidate->course, 'resource_content', 0)) { + if ($file = resourcelib_try_file_migration($resource->mainfile, $candidate->cmid, $candidate->course, 'mod_resource', 'content', 0)) { $resource->mainfile = $file->get_filepath().$file->get_filename(); } } Index: mod/resource/db/upgrade.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/resource/db/upgrade.php,v retrieving revision 1.14 diff -u -r1.14 upgrade.php --- mod/resource/db/upgrade.php 28 May 2010 07:29:12 -0000 1.14 +++ mod/resource/db/upgrade.php 28 Jun 2010 22:32:56 -0000 @@ -224,11 +224,12 @@ if ($instances = $DB->get_recordset_sql($sql)) { foreach ($instances as $instance) { $context = get_context_instance(CONTEXT_MODULE, $instance->cmid); - $filearea = 'resource_content'; + $component = 'mod_resource'; + $filearea = 'content'; $itemid = 0; $filepath = file_correct_filepath(dirname($instance->mainfile)); $filename = basename($instance->mainfile); - file_set_sortorder($context->id, $filearea, $itemid, $filepath, $filename, 1); + file_set_sortorder($context->id, $component, $filearea, $itemid, $filepath, $filename, 1); } } Index: lib/db/install.xml =================================================================== RCS file: /cvsroot/moodle/moodle/lib/db/install.xml,v retrieving revision 1.258 diff -u -r1.258 install.xml --- lib/db/install.xml 25 Jun 2010 11:36:53 -0000 1.258 +++ lib/db/install.xml 28 Jun 2010 22:32:24 -0000 @@ -1,5 +1,5 @@ - @@ -2262,13 +2262,14 @@
    - +
    - - + + + @@ -2289,8 +2290,8 @@ - - + +
    Index: lib/db/upgrade.php =================================================================== RCS file: /cvsroot/moodle/moodle/lib/db/upgrade.php,v retrieving revision 1.428 diff -u -r1.428 upgrade.php --- lib/db/upgrade.php 27 Jun 2010 09:30:59 -0000 1.428 +++ lib/db/upgrade.php 28 Jun 2010 22:32:29 -0000 @@ -497,6 +497,7 @@ $table->add_field('contenthash', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null); $table->add_field('pathnamehash', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null); $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null); + $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null); $table->add_field('filearea', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null); $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null); $table->add_field('filepath', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null); @@ -517,7 +518,7 @@ $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id')); /// Adding indexes to table files - $table->add_index('filearea-contextid-itemid', XMLDB_INDEX_NOTUNIQUE, array('filearea', 'contextid', 'itemid')); + $table->add_index('component-filearea-contextid-itemid', XMLDB_INDEX_NOTUNIQUE, array('component', 'filearea', 'contextid', 'itemid')); $table->add_index('contenthash', XMLDB_INDEX_NOTUNIQUE, array('contenthash')); $table->add_index('pathnamehash', XMLDB_INDEX_UNIQUE, array('pathnamehash')); @@ -4846,6 +4847,64 @@ upgrade_main_savepoint($result, 2010062101); } + if ($result && $oldversion < 2010062600.01) { + //TODO: this is a temporary hack for upgrade from PR3, to be removed later + + // Define field component to be added to files + $table = new xmldb_table('files'); + $field = new xmldb_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'contextid'); + + // Conditionally upgrade from PR3 + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + $index = new xmldb_index('filearea-contextid-itemid', XMLDB_INDEX_NOTUNIQUE, array('filearea', 'contextid', 'itemid')); + $dbman->drop_index($table, $index); + $index = new xmldb_index('component-filearea-contextid-itemid', XMLDB_INDEX_NOTUNIQUE, array('component', 'filearea', 'contextid', 'itemid')); + $dbman->add_index($table, $index); + + // Rename areas as add proper component + $areas = $DB->get_fieldset_sql("SELECT DISTINCT filearea FROM {files}"); + if ($areas) { + // fix incorrect itemids + $DB->execute("UPDATE {files} SET itemid = 0 WHERE filearea = 'category_description'"); // context identifies instances + $DB->execute("UPDATE {files} SET itemid = 0 WHERE filearea = 'user_profile'"); // context identifies instances + $DB->execute("UPDATE {files} SET itemid = 0 WHERE filearea = 'block_html'"); // context identifies instances + foreach ($areas as $area) { + // rename areas + if ($area === 'course_backup') { + $area = 'backup_course'; + } else if ($area === 'section_backup') { + $area = 'backup_section'; + } else if ($area === 'activity_backup') { + $area = 'backup_activity'; + } else if ($area === 'category_description') { + $area = 'coursecat_description'; + } + if ($area === 'block_html') { + $component = 'block_html'; + $filearea = 'content'; + } else { + list($component, $filearea) = explode('_', $area, 2); + // note this is just a bloody hack which guesses plugin from old PRE3 files code, the whole point of adding component is to get rid of this guessing + if (file_exists("$CFG->dirroot/mod/$component/lib.php")) { + $component = 'mod_'.$component; + } + } + $DB->execute("UPDATE {files} SET component = :component, filearea = :filearea WHERE filearea = :area", array('component'=>$component, 'filearea'=>$filearea, 'area'=>$area)); + } + // Update all hashes + $rs = $DB->get_recordset('files', array()); + foreach ($rs as $file) { + $pathnamehash = sha1("/$file->contextid/$file->component/$file->filearea/$file->itemid".$file->filepath.$file->filename); + $DB->set_field('files', 'pathnamehash', $pathnamehash, array('id'=>$file->id)); + } + $rs->close(); + } + } + + // Main savepoint reached + upgrade_main_savepoint($result, 2010062600.01); + } return $result; Index: lib/db/upgradelib.php =================================================================== RCS file: /cvsroot/moodle/moodle/lib/db/upgradelib.php,v retrieving revision 1.37 diff -u -r1.37 upgradelib.php --- lib/db/upgradelib.php 20 May 2010 09:09:08 -0000 1.37 +++ lib/db/upgradelib.php 28 Jun 2010 22:32:29 -0000 @@ -105,11 +105,13 @@ } if (strpos($path, '/backupdata/') === 0) { - $filearea = 'course_backup'; - $filepath = substr($path, strlen('/backupdata')); + $component = 'backup'; + $filearea = 'course'; + $filepath = substr($path, strlen('/backupdata')); } else { - $filearea = 'course_content'; - $filepath = $path; + $component = 'course'; + $filearea = 'legacy'; + $filepath = $path; } if ($item->isFile()) { @@ -126,8 +128,8 @@ continue; } - if (!$fs->file_exists($context->id, $filearea, '0', $filepath, $filename)) { - $file_record = array('contextid'=>$context->id, 'filearea'=>$filearea, 'itemid'=>0, 'filepath'=>$filepath, 'filename'=>$filename, + if (!$fs->file_exists($context->id, $component, $filearea, '0', $filepath, $filename)) { + $file_record = array('contextid'=>$context->id, 'component'=>$component, 'filearea'=>$filearea, 'itemid'=>0, 'filepath'=>$filepath, 'filename'=>$filename, 'timecreated'=>$item->getCTime(), 'timemodified'=>$item->getMTime()); if ($fs->create_file_from_pathname($file_record, $fullpathname.$item->getFilename())) { if ($delete_this) { @@ -148,7 +150,7 @@ } $filepath = ($filepath.$dirname.'/'); if ($filepath !== '/backupdata/') { - $fs->create_directory($context->id, $filearea, 0, $filepath); + $fs->create_directory($context->id, $component, $filearea, 0, $filepath); } //migrate recursively all subdirectories @@ -203,8 +205,8 @@ continue; } - if (!$fs->file_exists(SYSCONTEXTID, 'blog', $entry->id, '/', $filename)) { - $file_record = array('contextid'=>SYSCONTEXTID, 'filearea'=>'blog_attachment', 'itemid'=>$entry->id, 'filepath'=>'/', 'filename'=>$filename, + if (!$fs->file_exists(SYSCONTEXTID, 'blog', 'attachment', $entry->id, '/', $filename)) { + $file_record = array('contextid'=>SYSCONTEXTID, 'component'=>'blog', 'filearea'=>'attachment', 'itemid'=>$entry->id, 'filepath'=>'/', 'filename'=>$filename, 'timecreated'=>filectime($pathname), 'timemodified'=>filemtime($pathname), 'userid'=>$entry->userid); $fs->create_file_from_pathname($file_record, $pathname); } Index: mod/page/db/upgradelib.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/page/db/upgradelib.php,v retrieving revision 1.1 diff -u -r1.1 upgradelib.php --- mod/page/db/upgradelib.php 13 Aug 2009 20:49:08 -0000 1.1 +++ mod/page/db/upgradelib.php 28 Jun 2010 22:32:55 -0000 @@ -111,15 +111,15 @@ $page = resource_migrate_to_module('page', $candidate, $page); // now try to migrate files from site files - // noite: this can not work for html pages or files with other relatively linked files :-( + // note: this can not work for html pages or files with other relatively linked files :-( $siteid = get_site()->id; if (preg_match_all("|$CFG->wwwroot/file.php(\?file=)?/$siteid(/[^\s'\"&\?#]+)|", $page->content, $matches)) { $context = get_context_instance(CONTEXT_MODULE, $candidate->cmid); $sitecontext = get_context_instance(CONTEXT_COURSE, $siteid); - $file_record = array('contextid'=>$context->id, 'filearea'=>'page_content', 'itemid'=>0); + $file_record = array('contextid'=>$context->id, 'component'=>'mod_page', 'filearea'=>'content', 'itemid'=>0); $fs = get_file_storage(); foreach ($matches[2] as $i=>$sitefile) { - if (!$file = $fs->get_file_by_hash(sha1($sitecontext->id.'course_content0'.$sitefile))) { + if (!$file = $fs->get_file_by_hash(sha1("/$sitecontext->id/course/legacy/0".$sitefile))) { continue; } try { Index: mod/forum/backup/moodle2/backup_forum_stepslib.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/forum/backup/moodle2/backup_forum_stepslib.php,v retrieving revision 1.1 diff -u -r1.1 backup_forum_stepslib.php --- mod/forum/backup/moodle2/backup_forum_stepslib.php 21 Apr 2010 09:35:30 -0000 1.1 +++ mod/forum/backup/moodle2/backup_forum_stepslib.php 28 Jun 2010 22:32:48 -0000 @@ -134,9 +134,10 @@ // Define file annotations - $forum->annotate_files(array('forum_intro'), null); // This file area hasn't itemid + $forum->annotate_files('mod_forum', 'intro', null); // This file area hasn't itemid - $post->annotate_files(array('forum_post', 'forum_attachment'), 'id'); + $post->annotate_files('mod_forum', 'post', 'id'); + $post->annotate_files('mod_forum', 'attachment', 'id'); // Return the root element (forum), wrapped into standard activity structure return $this->prepare_activity_structure($forum); Index: mod/folder/backup/moodle2/backup_folder_stepslib.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/folder/backup/moodle2/backup_folder_stepslib.php,v retrieving revision 1.1 diff -u -r1.1 backup_folder_stepslib.php --- mod/folder/backup/moodle2/backup_folder_stepslib.php 3 May 2010 17:42:19 -0000 1.1 +++ mod/folder/backup/moodle2/backup_folder_stepslib.php 28 Jun 2010 22:32:41 -0000 @@ -51,7 +51,8 @@ // (none) // Define file annotations - $folder->annotate_files(array('folder_intro', 'folder_content'), null); // This file area hasn't itemid + $folder->annotate_files('mod_folder', 'intro', null); + $folder->annotate_files('mod_folder', 'content', null); // Return the root element (folder), wrapped into standard activity structure return $this->prepare_activity_structure($folder); Index: lib/formslib.php =================================================================== RCS file: /cvsroot/moodle/moodle/lib/formslib.php,v retrieving revision 1.216 diff -u -r1.216 formslib.php --- lib/formslib.php 28 Jun 2010 14:36:51 -0000 1.216 +++ lib/formslib.php 28 Jun 2010 22:32:03 -0000 @@ -562,7 +562,7 @@ $draftid = $values[$elname]; $fs = get_file_storage(); $context = get_context_instance(CONTEXT_USER, $USER->id); - if (!$files = $fs->get_area_files($context->id, 'user_draft', $draftid, 'id DESC', false)) { + if (!$files = $fs->get_area_files($context->id, 'user', 'draft', $draftid, 'id DESC', false)) { return false; } $file = reset($files); @@ -612,7 +612,7 @@ $draftid = $values[$elname]; $fs = get_file_storage(); $context = get_context_instance(CONTEXT_USER, $USER->id); - if (!$files = $fs->get_area_files($context->id, 'user_draft', $draftid, 'id DESC', false)) { + if (!$files = $fs->get_area_files($context->id, 'user', 'draft', $draftid, 'id DESC', false)) { return false; } $file = reset($files); @@ -651,7 +651,7 @@ $draftid = $values[$elname]; $fs = get_file_storage(); $context = get_context_instance(CONTEXT_USER, $USER->id); - if (!$files = $fs->get_area_files($context->id, 'user_draft', $draftid, 'id DESC', false)) { + if (!$files = $fs->get_area_files($context->id, 'user', 'draft', $draftid, 'id DESC', false)) { return null; } return $files; @@ -660,33 +660,6 @@ } /** - * Dispose form element draft files - * - * @global object $USER - * @param string $elname name of element - */ - function dispose($elname) { - global $USER; - - if (!$this->is_submitted() or !$this->is_validated()) { - return false; - } - - $element = $this->_form->getElement($elname); - - if ($element instanceof MoodleQuickForm_filepicker || $element instanceof MoodleQuickForm_filemanager) { - $values = $this->_form->exportValues($elname); - if (empty($values[$elname])) { - return false; - } - $draftid = $values[$elname]; - $fs = get_file_storage(); - $context = get_context_instance(CONTEXT_USER, $USER->id); - $fs->delete_area_files($context->id, 'user_draft', $draftid); - } - } - - /** * Save file to local filesystem pool * * @global object @@ -699,7 +672,7 @@ * @param int $newuserid - new userid if required * @return mixed stored_file object or false if error; may throw exception if duplicate found */ - function save_stored_file($elname, $newcontextid, $newfilearea, $newitemid, $newfilepath='/', + function save_stored_file($elname, $newcontextid, $newcomponent, $newfilearea, $newitemid, $newfilepath='/', $newfilename=null, $overwrite=false, $newuserid=null) { global $USER; @@ -721,7 +694,7 @@ } $draftid = $values[$elname]; $context = get_context_instance(CONTEXT_USER, $USER->id); - if (!$files = $fs->get_area_files($context->id, 'user_draft', $draftid, 'id DESC', false)) { + if (!$files = $fs->get_area_files($context->id, 'user' ,'draft', $draftid, 'id DESC', false)) { return false; } $file = reset($files); @@ -730,14 +703,14 @@ } if ($overwrite) { - if ($oldfile = $fs->get_file($newcontextid, $newfilearea, $newitemid, $newfilepath, $newfilename)) { + if ($oldfile = $fs->get_file($newcontextid, $newcomponent, $newfilearea, $newitemid, $newfilepath, $newfilename)) { if (!$oldfile->delete()) { return false; } } } - $file_record = array('contextid'=>$newcontextid, 'filearea'=>$newfilearea, 'itemid'=>$newitemid, + $file_record = array('contextid'=>$newcontextid, 'component'=>$newcomponent, 'filearea'=>$newfilearea, 'itemid'=>$newitemid, 'filepath'=>$newfilepath, 'filename'=>$newfilename, 'userid'=>$newuserid); return $fs->create_file_from_storedfile($file_record, $file); @@ -745,14 +718,14 @@ $filename = is_null($newfilename) ? $_FILES[$elname]['name'] : $newfilename; if ($overwrite) { - if ($oldfile = $fs->get_file($newcontextid, $newfilearea, $newitemid, $newfilepath, $newfilename)) { + if ($oldfile = $fs->get_file($newcontextid, $newcomponent, $newfilearea, $newitemid, $newfilepath, $newfilename)) { if (!$oldfile->delete()) { return false; } } } - $file_record = array('contextid'=>$newcontextid, 'filearea'=>$newfilearea, 'itemid'=>$newitemid, + $file_record = array('contextid'=>$newcontextid, 'component'=>$newcomponent, 'filearea'=>$newfilearea, 'itemid'=>$newitemid, 'filepath'=>$newfilepath, 'filename'=>$newfilename, 'userid'=>$newuserid); return $fs->create_file_from_pathname($file_record, $_FILES[$elname]['tmp_name']); } @@ -784,7 +757,7 @@ $draftid = $values[$elname]; $fs = get_file_storage(); $context = get_context_instance(CONTEXT_USER, $USER->id); - if (!$files = $fs->get_area_files($context->id, 'user_draft', $draftid, 'id DESC', false)) { + if (!$files = $fs->get_area_files($context->id, 'user', 'draft', $draftid, 'id DESC', false)) { return false; } $file = reset($files); Index: lib/outputcomponents.php =================================================================== RCS file: /cvsroot/moodle/moodle/lib/outputcomponents.php,v retrieving revision 1.133 diff -u -r1.133 outputcomponents.php --- lib/outputcomponents.php 22 Jun 2010 09:04:46 -0000 1.133 +++ lib/outputcomponents.php 28 Jun 2010 22:32:13 -0000 @@ -36,144 +36,6 @@ } /** - * Data structure representing a area file tree viewer - * - * @copyright 2010 Dongsheng Cai - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * @since Moodle 2.0 - */ -class area_file_tree_viewer implements renderable { - public $dir; - public $result; - public $filearea; - /** - * Constructor of area_file_tree_viewer class - * @param int $contextid - * @param string $area, file area - * @param int $itemid - * @param string $urlbase, file serving url base - */ - public function __construct($contextid, $area, $itemid, $urlbase='') { - global $CFG; - $fs = get_file_storage(); - if (empty($urlbase)) { - $this->urlbase = "$CFG->wwwroot/pluginfile.php"; - } else { - $this->urlbase = $urlbase; - } - $this->contextid = $contextid; - $this->filearea = $area; - $this->itemid = $itemid; - $this->dir = $fs->get_area_tree($contextid, $area, $itemid); - $this->tree_view_parser($this->dir); - } - /** - * Pre-process file tree, generate file url - * @param array $dir file tree - */ - public function tree_view_parser($dir) { - if (empty($dir['subdirs']) and empty($dir['files'])) { - return null; - } - foreach ($dir['subdirs'] as $subdir) { - $this->tree_view_parser($subdir); - } - foreach ($dir['files'] as $file) { - $path = '/'.$this->contextid.'/'.$this->filearea.'/'.$this->itemid.$file->get_filepath().$file->get_filename(); - $downloadurl = file_encode_url($this->urlbase, $path, true); - $file->fileurl = $downloadurl; - } - } -} - -/** - * Data structure representing a general moodle file tree viewer - * - * @copyright 2010 Dongsheng Cai - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * @since Moodle 2.0 - */ -class moodle_file_tree_viewer implements renderable { - public $tree; - public $path; - private $enabled_fileareas; - /** - * Constructor of moodle_file_tree_viewer class - * @param int $contextid - * @param string $area, file area - * @param int $itemid - * @param string $urlbase, file serving url base - */ - public function __construct($contextid, $filearea, $itemid, $filepath, $options=array()) { - global $CFG, $OUTPUT; - $this->tree = array(); - $browser = get_file_browser(); - $fs = get_file_storage(); - $fileinfo = $browser->get_file_info(get_context_instance_by_id($contextid), $filearea, $itemid, $filepath); - $children = $fileinfo->get_children(); - $parent_info = $fileinfo->get_parent(); - if (!empty($options['enabled_fileareas']) && is_array($options['enabled_fileareas'])) { - $this->enabled_fileareas = $options['enabled_fileareas']; - } else { - unset($this->enabled_fileareas); - } - - $level = $parent_info; - $this->path = array(); - while ($level) { - $params = $level->get_params(); - $context = get_context_instance_by_id($params['contextid']); - // lock user in course level - if ($context->contextlevel == CONTEXT_COURSECAT or $context->contextlevel == CONTEXT_SYSTEM) { - break; - } - $url = new moodle_url('/files/index.php', $params); - $this->path[] = html_writer::link($url->out(false), $level->get_visible_name()); - $level = $level->get_parent(); - } - $this->path = array_reverse($this->path); - $this->path[] = $fileinfo->get_visible_name(); - - foreach ($children as $child) { - $filedate = $child->get_timemodified(); - $filesize = $child->get_filesize(); - $mimetype = $child->get_mimetype(); - $params = $child->get_params(); - $url = new moodle_url('/files/index.php', $params); - $fileitem = array( - 'params'=>$params, - 'filename'=>$child->get_visible_name(), - 'filedate'=>$filedate ? userdate($filedate) : '', - 'filesize'=>$filesize ? display_size($filesize) : '' - ); - if ($child->is_directory()) { - $fileitem['isdir'] = true; - $fileitem['url'] = $url->out(false); - if (isset($this->enabled_fileareas)) { - if (!in_array($params['filearea'], $this->enabled_fileareas)) { - continue; - } else { - if (!empty($params['itemid'])) { - $itemid = $params['itemid']; - } else { - $itemid = false; - } - $draftfiles = $fs->get_area_files($contextid, $params['filearea'], $itemid, 'id', false); - if (count($draftfiles) == 0) { - continue; - } - } - } - } else { - $fileitem['url'] = $child->get_url(); - } - $this->tree[] = $fileitem; - } - - } -} - -/** * Data structure representing a file picker. * * @copyright 2010 Dongsheng Cai @@ -187,7 +49,7 @@ require_once($CFG->dirroot. '/repository/lib.php'); $defaults = array( 'accepted_types'=>'*', - 'context'=>$PAGE->context, + 'context'=>$PAGE->context, //TODO: no PAGE in components allowed!! (skodak) 'return_types'=>FILE_INTERNAL, 'env' => 'filepicker', 'client_id' => uniqid(), @@ -206,14 +68,14 @@ $fs = get_file_storage(); $usercontext = get_context_instance(CONTEXT_USER, $USER->id); if (empty($options->filename)) { - if ($files = $fs->get_area_files($usercontext->id, 'user_draft', $options->itemid, 'id DESC', false)) { + if ($files = $fs->get_area_files($usercontext->id, 'user', 'draft', $options->itemid, 'id DESC', false)) { $file = reset($files); } } else { - $file = $fs->get_file($usercontext->id, 'user_draft', $options->itemid, $options->filepath, $options->filename); + $file = $fs->get_file($usercontext->id, 'user', 'draft', $options->itemid, $options->filepath, $options->filename); } if (!empty($file)) { - $options->currentfile = html_writer::link(file_encode_url($CFG->wwwroot.'/draftfile.php/', $usercontext->id.'/user_draft/'.$file->get_itemid().'/'.$file->get_filename()), $file->get_filename()); + $options->currentfile = html_writer::link(file_draftfile_url($file->get_itemid(), $file->get_filename(), $file->get_filename()), $file->get_filename()); } } @@ -228,63 +90,6 @@ } /** - * Data structure representing a file manager. - * - * @copyright 2010 Dongsheng Cai - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * @since Moodle 2.0 - */ -class file_manager implements renderable { - public $options; - public function __construct(stdClass $options) { - global $CFG, $USER, $PAGE; - require_once($CFG->dirroot. '/repository/lib.php'); - $defaults = array( - 'maxbytes'=>-1, - 'maxfiles'=>-1, - 'filearea'=>'user_draft', - 'itemid'=>0, - 'subdirs'=>0, - 'client_id'=>uniqid(), - 'accepted_types'=>'*', - 'return_types'=>FILE_INTERNAL, - 'context'=>$PAGE->context - ); - foreach ($defaults as $key=>$value) { - if (empty($options->$key)) { - $options->$key = $value; - } - } - - $fs = get_file_storage(); - - // initilise options, getting files in root path - $this->options = file_get_user_area_files($options->itemid, '/', $options->filearea); - - // calculate file count - $usercontext = get_context_instance(CONTEXT_USER, $USER->id); - $files = $fs->get_area_files($usercontext->id, $options->filearea, $options->itemid, 'id', false); - $filecount = count($files); - $this->options->filecount = $filecount; - - // copying other options - foreach ($options as $name=>$value) { - $this->options->$name = $value; - } - - // building file picker options - $params = new stdclass; - $params->accepted_types = $options->accepted_types; - $params->return_types = $options->return_types; - $params->context = $options->context; - $params->env = 'filemanager'; - $params->disable_types = !empty($options->disable_types)?$options->disable_types:array(); - $filepicker_options = initialise_filepicker($params); - $this->options->filepicker = $filepicker_options; - } -} - -/** * Data structure representing a user picture. * * @copyright 2009 Nicolas Connault, 2010 Petr Skoda Index: lib/setuplib.php =================================================================== RCS file: /cvsroot/moodle/moodle/lib/setuplib.php,v retrieving revision 1.107 diff -u -r1.107 setuplib.php --- lib/setuplib.php 21 Jun 2010 15:30:50 -0000 1.107 +++ lib/setuplib.php 28 Jun 2010 22:32:18 -0000 @@ -800,7 +800,7 @@ */ function redirect_if_major_upgrade_required() { global $CFG; - $lastmajordbchanges = 2010061600; + $lastmajordbchanges = 2010062900; if (empty($CFG->version) or (int)$CFG->version < $lastmajordbchanges or during_initial_install() or !empty($CFG->adminsetuppending)) { try { Index: lib/resourcelib.php =================================================================== RCS file: /cvsroot/moodle/moodle/lib/resourcelib.php,v retrieving revision 1.10 diff -u -r1.10 resourcelib.php --- lib/resourcelib.php 1 Jun 2010 03:53:15 -0000 1.10 +++ lib/resourcelib.php 28 Jun 2010 22:32:16 -0000 @@ -51,11 +51,12 @@ * @param string $filepath old file path * @param int $cmid migrated course module if * @param int $courseid + * @param string $component * @param string $filearea new file area * @param int $itemid migrated file item id * @return mixed, false if not found, stored_file instance if migrated to new area */ -function resourcelib_try_file_migration($filepath, $cmid, $courseid, $filearea, $itemid) { +function resourcelib_try_file_migration($filepath, $cmid, $courseid, $component, $filearea, $itemid) { $fs = get_file_storage(); if (stripos($filepath, '/backupdata/') === 0 or stripos($filepath, '/moddata/') === 0) { @@ -70,13 +71,13 @@ return false; } - $pathnamehash = sha1($coursecontext->id.'course_content0'.$filepath); + $pathnamehash = sha1("/$coursecontext->id/course/legacy/0".$filepath); if (!$file = $fs->get_file_by_hash($pathnamehash)) { return false; } // copy and keep the same path, name, etc. - $file_record = array('contextid'=>$context->id, 'filearea'=>$filearea, 'itemid'=>$itemid); + $file_record = array('contextid'=>$context->id, 'component'=>$component, 'filearea'=>$filearea, 'itemid'=>$itemid); try { return $fs->create_file_from_storedfile($file_record, $file); } catch (Exception $e) { Index: lib/navigationlib.php =================================================================== RCS file: /cvsroot/moodle/moodle/lib/navigationlib.php,v retrieving revision 1.145 diff -u -r1.145 navigationlib.php --- lib/navigationlib.php 28 Jun 2010 02:32:27 -0000 1.145 +++ lib/navigationlib.php 28 Jun 2010 22:32:11 -0000 @@ -2660,7 +2660,7 @@ // Restore to this course if (has_capability('moodle/restore:restorecourse', $coursecontext)) { - $url = new moodle_url('/files/index.php', array('id'=>$course->id, 'wdir'=>'/backupdata')); + $url = new moodle_url('/files/index.php', array('contextid'=>$coursecontext->id, 'itemid'=>0, 'component' => 'backup', 'filearea'=>'course')); $url = null; // Disabled until restore is implemented. MDL-21432 $coursenode->add(get_string('restore'), $url, self::TYPE_SETTING, null, 'restore', new pix_icon('i/restore', '')); } @@ -2698,7 +2698,7 @@ // Manage files if ($course->legacyfiles == 2 and has_capability('moodle/course:managefiles', $coursecontext)) { - $url = new moodle_url('/files/index.php', array('contextid'=>$coursecontext->id, 'itemid'=>0, 'filearea'=>'course_content')); + $url = new moodle_url('/files/index.php', array('contextid'=>$coursecontext->id, 'itemid'=>0, 'component' => 'course', 'filearea'=>'legacy')); $coursenode->add(get_string('files'), $url, self::TYPE_SETTING, null, 'coursefiles', new pix_icon('i/files', '')); } @@ -3318,7 +3318,8 @@ // Restore to this course if (has_capability('moodle/restore:restorecourse', $coursecontext)) { - $url = new moodle_url('/files/index.php', array('id'=>$course->id, 'wdir'=>'/backupdata')); + $url = new moodle_url('/files/index.php', array('contextid'=>$coursecontext->id, 'itemid'=>0, 'component' => 'backup', 'filearea'=>'course')); + $url = null; // Disabled until restore is implemented. MDL-21432 $frontpage->add(get_string('restore'), $url, self::TYPE_SETTING, null, null, new pix_icon('i/restore', '')); } @@ -3343,7 +3344,8 @@ // Manage files if (has_capability('moodle/course:managefiles', $this->context)) { - $url = new moodle_url('/files/index.php', array('id'=>$course->id)); + //TODO: hide in new installs + $url = new moodle_url('/files/index.php', array('contextid'=>$coursecontext->id, 'itemid'=>0, 'component' => 'course', 'filearea'=>'legacy')); $frontpage->add(get_string('files'), $url, self::TYPE_SETTING, null, null, new pix_icon('i/files', '')); } return $frontpage; Index: lib/outputrenderers.php =================================================================== RCS file: /cvsroot/moodle/moodle/lib/outputrenderers.php,v retrieving revision 1.220 diff -u -r1.220 outputrenderers.php --- lib/outputrenderers.php 23 Jun 2010 09:16:53 -0000 1.220 +++ lib/outputrenderers.php 28 Jun 2010 22:32:15 -0000 @@ -1769,88 +1769,6 @@ return html_writer::tag('a', $output, $attributes); } - - /** - * General moodle file tree viwer - * - *
    -     * $OUTPUT->moodle_file_tree_viewer($contextid, $filearea, $itemid, $filepath);
    -     * 
    - * - * @param int $contextid - * @param string $area - * @param int $itemid - * @param string $filepath - * @return string HTML fragment - */ - public function moodle_file_tree_viewer($contextid, $filearea, $itemid, $filepath, $options = array()) { - $tree = new moodle_file_tree_viewer($contextid, $filearea, $itemid, $filepath, $options); - return $this->render($tree); - } - public function render_moodle_file_tree_viewer(moodle_file_tree_viewer $tree) { - $html = '
    '; - foreach($tree->path as $path) { - $html .= $path; - $html .= ' / '; - } - $html .= '
    '; - - $html .= '
    '; - if (empty($tree->tree)) { - $html .= get_string('nofilesavailable', 'repository'); - } else { - $this->page->requires->js_init_call('M.core_filetree.init'); - $html .= '
      '; - foreach($tree->tree as $node) { - $link_attributes = array(); - if (!empty($node['isdir'])) { - $class = ' class="file-tree-folder"'; - } else { - $class = ' class="file-tree-file"'; - $link_attributes['target'] = '_blank'; - } - $html .= '
    • '; - $html .= html_writer::link($node['url'], $node['filename'], $link_attributes); - $html .= '
    • '; - } - $html .= '
    '; - } - $html .= '
    '; - return $html; - } - - /** - * Print the area file tree viewer - * - *
    -     * $OUTPUT->area_file_tree_viewer($contextid, $filearea, $itemid, $urlbase);
    -     * 
    - * - * @param int $contextid - * @param string $area - * @param int $itemid - * @param string $urlbase - * @return string HTML fragment - */ - public function area_file_tree_viewer($contextid, $area, $itemid, $urlbase='') { - $tree = new area_file_tree_viewer($contextid, $area, $itemid, $urlbase); - return $this->render($tree); - } - - /** - * Internal implementation of area file tree viewer rendering. - * @param area_file_tree_viewer $tree - * @return string - */ - public function render_area_file_tree_viewer(area_file_tree_viewer $tree) { - $this->page->requires->js_init_call('M.mod_folder.init_tree', array(true)); - $html = ''; - $html .= '
    '; - $html .= $this->htmllize_file_tree($tree->dir); - $html .= '
    '; - return $html; - } - /** * Internal implementation of file tree viewer items rendering. * @param array $dir @@ -1930,135 +1848,6 @@ } /** - * Print the file manager - * - *
    -     * $OUTPUT->file_manager($options);
    -     * 
    - * - * @param array $options associative array with file manager options - * options are: - * maxbytes=>-1, - * maxfiles=>-1, - * filearea=>'user_draft', - * itemid=>0, - * subdirs=>false, - * client_id=>uniqid(), - * acepted_types=>'*', - * return_types=>FILE_INTERNAL, - * context=>$PAGE->context - * @return string HTML fragment - */ - public function file_manager($options) { - $fm = new file_manager($options); - return $this->render($fm); - } - - /** - * Internal implementation of file manager rendering. - * @param file_manager $fm - * @return string - */ - public function render_file_manager(file_manager $fm) { - global $CFG, $OUTPUT; - static $filemanagertemplateloaded; - $html = ''; - $nonjsfilemanager = optional_param('usenonjsfilemanager', 0, PARAM_INT); - $options = $fm->options; - $options->usenonjs = $nonjsfilemanager; - $straddfile = get_string('add', 'repository') . '...'; - $strmakedir = get_string('makeafolder', 'moodle'); - $strdownload = get_string('downloadfolder', 'repository'); - $strloading = get_string('loading', 'repository'); - - $icon_add_file = $OUTPUT->pix_icon('t/addfile', $straddfile).''; - $icon_add_folder = $OUTPUT->pix_icon('t/adddir', $strmakedir).''; - $icon_download = $OUTPUT->pix_icon('t/download', $strdownload).''; - $icon_progress = $OUTPUT->pix_icon('i/loading_small', $strloading).''; - - $client_id = $options->client_id; - $itemid = $options->itemid; - $filearea = $options->filearea; - - if (empty($options->filecount)) { - $extra = ' style="display:none"'; - } else { - $extra = ''; - } - - $html .= << -$icon_progress -
  • - -
    -FMHTML; - if (empty($filemanagertemplateloaded)) { - $filemanagertemplateloaded = true; - $html .= <<